note 56156 added to function.date
| From: | sandved at gmail dot com | Date: | Thu, 25 Aug 2005 09:15:37 +0000 |
| Subject: | note 56156 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94171@lists.php.net to get a copy of this message | ||
The format parameter 'W' which return the ISO Week, do not follow the ISO standard (ISO
8601), since it return the first 9 weeks without a leading zero.
Use this for returning current week with leading zeros
$ISOweek = substr('0' . date('W') , -2);
Here's an example returning the ISO year if you're running an earlier versiuon of PHP than
5.1 (were tou can use the parameter 'o'):
(I'm quite new to PHP, so the code can be a bit 'bad')
<?php
//if OK
// $datetext = "28 December 2003"; //2003-W52-7
// $datetext = "29 December 2003"; //2004-W01-1
//elseif OK
$datetext = "3 January 1999"; //1998-W53-7
// $datetext = "4 January 1999"; //1999-W01-1
//
$ISOweek = substr('0' . date('W',strtotime($datetext)) , -2);
if ((date('d',strtotime($datetext))>=29) &&
(date('m',strtotime($datetext))==12) &&
(date('W',strtotime($datetext))==1)) {
$ISOyearweek = date('Y',strtotime($datetext)) + 1 . '-W' . $ISOweek;
} elseif ((date('d',strtotime($datetext))<=3) &&
(date('m',strtotime($datetext))==1) &&
(date('W',strtotime($datetext))==53)) {
$ISOyearweek = date('Y',strtotime($datetext)) - 1 . '-W' . $ISOweek;
} else {
$ISOyearweek = date('Y',strtotime($datetext)) . ' -W' . $ISOweek;
}
echo $datetext,"<BR>";
echo "Date: " . date('Y-m-d',strtotime($datetext)),"<BR>";
echo "Week: " . $ISOyearweek,"<BR>";
?>
More on converting in accordance with ISO 8601, see
http://www.pvv.org/~nsaa/ISO8601.html
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56156
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56156&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56156&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56156&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56156&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56156&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56156&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56156&report=yes
Search -- http://master.php.net/manage/user-notes.php