The First Week of the Year (According to PHP) March 2nd, 2011
So if January 1st, 2011 isn't part of the first week of the new year, what week does it belong to? The answer is "52." January 1st 2011 is part of the 52nd week of 2010. It seems kinda weird at first, but it makes sense once you think about it for a bit. Since a week starts on a Monday, what happens when a new month starts in the middle of the week? Try out the following code:
$date = new DateTime('2011-01-01'); echo $date->format('W');
So What If I Want The Year Too?
Lets assume you want to group some data by year and week number.
If you use something like
date('Y-W') on January 1st, 2011 you will end up with
But as we just determined, January 1st is auctually the 52nd week of 2010.
Do not worry — PHP has your back.
$date = new DateTime('2011-01-01'); echo $date->format('o-W');
blog comments powered by Disqus