Skip to content


2009 has 53 weeks

I had to write a tool that will add weeks to a database, where records that get added in the future will have to be linked with those week records. So a cron job runs and creates about 150 weeks in advance, running once a month, leaving the previously inserted weeks alone.

Upon testing though there was a weird little snag.

What happened was that the insert scripts failed to find a particular week. Debug revealed that they tried to enter a week 53. But a year has only 52 weeks, right? Wrong. 2009 has actually 53 calendar weeks. So to make it future proof for when such a thing might happen again, here my little code that determines the number of possible weeks in the current year. You need nothing else than the date function to make it work.

$currentYear = date('Y');
$weeksInYear = date('W',mktime(23,59,59,12,31,$currentYear));

This should run fine until someone decides to change the number of days in December, number of months in a year, number of hours in a day or something equally constant.

Posted in PHP.