note 71842 added to function.strftime
| From: | commander at graphicore dot de | Date: | Tue, 19 Dec 2006 05:18:30 +0000 |
| Subject: | note 71842 added to function.strftime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120762@lists.php.net to get a copy of this message | ||
Thats how I get the %G, %g ISO Year and %V Week with php 4.4.4 on Window$.
It works quite simple:
1. on January it can't be a week bigger than 51, thats what i've learned in school
2. on december many weeks have gone since the last New Year's Eve and therefore it cant be the
first week of the year
i hope it saves time
<?php
function YearOfWeek($format,$date = NULL){
if(!isset($date)){
$date = time();
}
list ($G,$V,$month) = explode('_',date ('Y_W_m',$date));
if( ($month === '01') && ($V >= 52 ) ){
$G--;
}else if( ($month === '12') && ($V === '01') ){
$G++;
}
$g = substr($G, -2, 2);
$format =
str_replace(array('%V','%G','%g'),array($V,$G,$g),$format);
return ($format);
}
$format = 'week %V of the year %G (%g), year %Y (%y)';
$date = mktime(0, 0, 0, 12, 31, 2008);
//week 01 of the year 2009 (09), year 2008 (08)
echo strftime(YearOfWeek($format,$date), $date) . "\n";
$date = mktime(0, 0, 0, 1, 1, 2010);
//week 53 of the year 2009 (09), year 2010 (10)
echo strftime(YearOfWeek($format,$date), $date) . "\n";
echo strftime(YearOfWeek($format)) . "\n";
//something like: week 51 of the year 2006 (06), year 2006 (06)
//depends on the day you do this
?>
----
Server IP: 217.160.72.57
Probable Submitter: 217.85.89.92
----
Manual Page -- http://www.php.net/manual/en/function.strftime.php
Edit -- https://master.php.net/note/edit/71842
Del: integrated -- https://master.php.net/note/delete/71842/integrated
Del: useless -- https://master.php.net/note/delete/71842/useless
Del: bad code -- https://master.php.net/note/delete/71842/bad+code
Del: spam -- https://master.php.net/note/delete/71842/spam
Del: non-english -- https://master.php.net/note/delete/71842/non-english
Del: in docs -- https://master.php.net/note/delete/71842/in+docs
Del: other reasons-- https://master.php.net/note/delete/71842
Reject -- https://master.php.net/note/reject/71842
Search -- https://master.php.net/manage/user-notes.php