note 71842 deleted from function.strftime by derick
| From: | derick@php.net | Date: | Sun, 11 May 2008 14:01:40 +0000 |
| Subject: | note 71842 deleted from function.strftime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-139272@lists.php.net to get a copy of this message | ||
Note Submitter: commander at graphicore dot de
----
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
?>