I have run into an interesting issue using PHP’s date() function. Haven’t had any luck locating a discussion of this on SO or using Google, but maybe someone else has run into the same issue before?
I am trying to get the YEAR and the WEEK OF THE YEAR for a given timestamp. This is the code I am using:
date("Y-\WW");
Which as of today correctly outputs: 2011-W39.
The problem is when supplying a timestamp, if the timestamp in question is, for example, on January 3rd, 2011 (which was actually part of the “52nd week of 2010”. PHP correctly returns W52, but it also returns 2011 as the year, instead of 2010.
date("Y-\WW", 1294016400);
Outputs: 2011-W52
Any idea on how to solve this problem? I should note that although in this case it would be easy to just compare that the strtotime() of the output is greater than the current time() and adjust, but I need a solution that will work for previous years as well (e.g. if the same thing happened for January 3rd 2010).
Darn, apparently I didn’t re-read the documentation closely enough, the letter
owas added in PHP 5.1.0:From the documentation for
date():So my code should have been
date("o-\WW");