2009 has 53 weeks
I was building a database tool for an application that tracks calendar weeks for planning purposes. The system was designed to automatically populate at least 150 weeks in advance via a monthly cron job.
Everything worked fine until I started importing data that referenced “week 53.” I was confused—years have 52 weeks, right?
Wrong. 2009 actually has 53 calendar weeks.
According to ISO-8601 week numbering, a week belongs to the year that contains most of its days. This means some years end up with 53 weeks instead of 52. It happens roughly every 5-6 years.
To future-proof the application, I wrote this simple PHP snippet to determine the actual number of weeks in any given year:
$weeksInYear = date('W', mktime(23, 59, 59, 12, 31, date('Y')));
This checks the week number of December 31st using PHP’s built-in date() function with ISO-8601 week numbering. No additional libraries or complex calculations needed.
Sometimes the bugs that teach you the most are the ones that make you question things you thought you knew.