note 56160 added to function.date
| From: | sandved at gmail dot com | Date: | Thu, 25 Aug 2005 11:30:57 +0000 |
| Subject: | note 56160 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-94178@lists.php.net to get a copy of this message | ||
The parameter 'd' returns 0 for sunday, 1 for monday up to 6 for saturday. According to
ISO (ISO 8601 - http://www.iso.org/iso/en/prods-services/popstds/datesandtime.html)
the week starts on monday and ends on sunday, numbered 1 to 7.
$ISOday = date('w');
//make sunday the last day in week, not the first as returned by the parameter 'w'
if ($ISOday == 0) {$ISOday = '7';}
Adjusted the code subitted in the posting below. Adjusted the year test:
<?php
/*
* Code to ekstract the ISO Week format YYYY-Www-D from a given date
* Here YYYY is the ISO year, ww the ISO week, and D day of week where 1-Monday and 7-Sunday
* For example will 1999-01-03 return 1998-W52-7
* See more about ISO 8601 and how to treat weeks at http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm
* and http://www.pvv.org/~nsaa/ISO8601.html
* Code done by nsaa@pvv.org 2005-08-25
*/
// test cases
//if
// $datetext = "28 December 2003"; //2003-W52-7
// $datetext = "29 December 2003"; //2004-W01-1
//elseif
$datetext = "3 January 1999"; //1998-W53-7
//$datetext = "4 January 1999"; //1999-W01-1
//$datetext = "2 January 2000"; //1999-W52-7
//$datetext = "3 January 2000"; //2000-W01-1
//
//Adds leading 0 to the week number
$ISOweek = substr('0' . date('W',strtotime($datetext)) , -2);
$ISOday = date('w',strtotime($datetext));
//make sunday the last day in week, not the first as returned by the parameter 'w'
if ($ISOday == 0) {$ISOday = '7';}
//Checks if the ISO Week 01 falls in desember the year before
if (((int)date('d',strtotime($datetext))>=29) &&
((int)date('m',strtotime($datetext))==12) &&
((int)date('W',strtotime($datetext))==1)) {
$ISOyear = date('Y',strtotime($datetext)) + 1;
//Checks if the ISO Week 52 or 53 falls in january the year after
} elseif (((int)date('d',strtotime($datetext))<=3) &&
((int)date('m',strtotime($datetext))==1) &&
((int)date('W',strtotime($datetext))>=52)) {
$ISOyear = date('Y',strtotime($datetext)) - 1;
} else {
$ISOyear = date('Y',strtotime($datetext));
}
//echo $datetext,"<BR>";
echo "Date : " . date('Y-m-d',strtotime($datetext)) ,"<BR>";
echo "Week : " . $ISOyear . "-" . $ISOweek ,"<BR>";
echo "Full week: " . $ISOyear . "-" . $ISOweek . "-" . $ISOday
,"<BR>";
?>
returns
Date : 1999-01-03
Week : 1998-53
Full week: 1998-53-7
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+56160
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+56160&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+56160&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+56160&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+56160&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+56160&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+56160&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+56160&report=yes
Search -- http://master.php.net/manage/user-notes.php