Week numbers in PHP

How to get the week number from a date

To get the ISO week number (1-53), use idate('W', $timestamp) or strftime('%-V', $timestamp). To get the week number with a leading zero (01-53), use strftime('%V', $timestamp).

$timestamp is a Unix timestamp, i.e. an integer value. The return value is relative to the default timezone.

To get the corresponding four-digit year (e.g. 2013), use strftime('%G', $timestamp).

Read more about strftime() and idate() in the PHP manual.

How to get the date from a week number

To get the Unix timestamp representing the start of the week (Monday at midnight), use strtotime(sprintf("%4dW%02d", $year, $week)).

$year is a 4-digit year (e.g. 2013), and $week is an ISO week number (1-53). The sprintf() call generates a week string in ISO 8601 format, e.g. "2013W01".

To get the start of the week as a DateTime object, use $date = new DateTime('midnight'); $date->setISODate($year, $week);

Read more about strtotime() in the PHP manual.

How to get the number of weeks in a year

To get the number of ISO weeks (i.e. the number of the last week) in a year, use idate('W', mktime(0, 0, 0, 12, 27, $year)).