note 126882 added to datetime.formats.date

From: Date: Fri, 25 Feb 2022 20:36:51 +0000
Subject: note 126882 added to datetime.formats.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-217660@lists.php.net to get a copy of this message
The ISO 8601 has the Week Date. I wrote this function. I hope that it can help someone. If second parameter is not empty it assumes that $a is the year, $b the month and $c the day, and return basic ISO week date: YYYYWwwD (default) or extended: YYYY-Www-D (if $x is true); If just first parameter is not empty assumes it is a ISO week date (basic or extended), and return the Unix timestamp. function ISOwd($a,$b='',$c='',$x=false) { if (!$b) { if (!preg_match_all('/([0-9]{4})-*W*([0-9]{2})-*([0-9]{1})/', $a, $m, PREG_SET_ORDER)) return null; /* Now year is $m[0][1], week is $m[0][2] and day is $m[0][3] */ $fd = mktime(0,0,0,1,1,$m[0][1]); // first day of the first week of the year $n = date('N', $fd); /* if ($n==7) $fd=2; if ($n==6) $fd=3; if ($n==5) $fd=4; if ($n==4) $fd=29; if ($n==3) $fd=30; if ($n==2) $fd=31; if ($n==1) $fd=1; */ if ($n>4) $fd = mktime(0,0,0,1,9-$n,$m[0][1]); elseif ($n>1) $fd = mktime(0,0,0,12,33-$n,$m[0][1]-1); return strtotime(date('d-m-Y',$fd).' + '.($m[0][2]-1).' week'.' + '.($m[0][3]-1).' days'); } $f = ($x) ? 'o-\WW-N' : 'o\WWN'; // basic / extended format return date($f, mktime(0,0,0,$b,$c,$a)); } // Example: echo $test = ISOwd(date('y'),date('m'),date('d')); echo '<br>'; echo date('y-m-d',ISOwd($test)); echo '<br>'; echo $test = ISOwd(date('y'),date('m'),date('d'),true); echo '<br>'; echo date('y-m-d',ISOwd($test)); ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.135 (proxied: 2803:9800:9016:4b8a:58e9:8cff:c5b7:29ce) ---- Manual Page -- https://php.net/manual/en/datetime.formats.date.php Edit -- https://main.php.net/note/edit/126882 Del: integrated -- https://main.php.net/note/delete/126882/integrated Del: useless -- https://main.php.net/note/delete/126882/useless Del: bad code -- https://main.php.net/note/delete/126882/bad+code Del: spam -- https://main.php.net/note/delete/126882/spam Del: non-english -- https://main.php.net/note/delete/126882/non-english Del: in docs -- https://main.php.net/note/delete/126882/in+docs Del: other reasons-- https://main.php.net/note/delete/126882 Reject -- https://main.php.net/note/reject/126882 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217660) next »