note 100385 added to function.strftime
| From: | Anonymous at osu1 dot php dot net | Date: | Tue, 12 Oct 2010 19:04:52 +0000 |
| Subject: | note 100385 added to function.strftime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173173@lists.php.net to get a copy of this message | ||
%V may fail with some systems (Windows XP, at least). Here is a function that should work to get the
week number of a day (timestamped), according to ISO 8601.
"should work" as in "it is working with my understanding of this norm", where
1st of january can be week 52, 53 or 01. Hopefully.
testing (php_uname("s") == "Windows NT") or equivalent can be an option (when
switching between Wampserver and a GNU server, for instance).
Please, erase and correct my message if there is any error.
<?php
function week_isonumber ($time) {
// When strftime("%V") fails, some unoptimized workaround
//
// http://en.wikipedia.org/wiki/ISO_8601 : week
1 is "the week with the year's first Thursday in it (the formal ISO definition)"
$year = strftime("%Y", $time);
$first_day = strftime("%w", mktime(0, 0, 0, 1, 1, $year));
$last_day = strftime("%w", mktime(0, 0, 0, 12, 31, $year));
$number = $isonumber = strftime("%W", $time);
// According to strftime("%W"), 1st of january is in week 1 if and only if it is a monday
if ($first_day == 1)
$isonumber--;
// 1st of january is between monday and thursday; starting (now) at 0 when it should be 1
if ($first_day >= 1 && $first_day <= 4)
$isonumber++;
else if ($number == 0)
$isonumber = week_isonumber(mktime(0, 0, 0, 12, 31, $year - 1));
if ($isonumber == 53 && ($last_day == 1 || $last_day == 2 || $last_day == 3))
$isonumber = 1;
return sprintf("%02d", $isonumber);
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 78.241.128.114
----
Manual Page -- http://www.php.net/manual/en/function.strftime.php
Edit -- https://master.php.net/note/edit/100385
Del: integrated -- https://master.php.net/note/delete/100385/integrated
Del: useless -- https://master.php.net/note/delete/100385/useless
Del: bad code -- https://master.php.net/note/delete/100385/bad+code
Del: spam -- https://master.php.net/note/delete/100385/spam
Del: non-english -- https://master.php.net/note/delete/100385/non-english
Del: in docs -- https://master.php.net/note/delete/100385/in+docs
Del: other reasons-- https://master.php.net/note/delete/100385
Reject -- https://master.php.net/note/reject/100385
Search -- https://master.php.net/manage/user-notes.php