1

I have this code

$week = date('W', strtotime("2013-12-30"));

and $week is returning 01

How can that return 01 if we are in December? How can I fix this? As this is impacting on another part of the code where I'm trying to get the monday of each week:

$monday = date("Y-m-d", strtotime("{$year}-W{$week}-1"));

But when I do that with that date, it's returning the monday of week 01.

2 Answers 2

3

date("W") returns the iso8601 week number. That date falls into the first week of the next year. This can occur when the last day(s) of December fall mid-week. This can also happen when December ends late in the week. The first couple of days of January can be considered part of the previous year.

See Week dates in Wikipedia for a better explanation:

If 1 January is on a Monday, Tuesday, Wednesday or Thursday, it is in week 01. If 1 January is on a Friday, Saturday or Sunday, it is in week 52 or 53 of the previous year (there is no week 00). 28 December is always in the last week of its year.

1

It is working correctly.

The "week number" is sometimes counterintuitive. See the note from "Jimmy" for the documentation of date using W format: http://www.php.net/manual/en/function.date.php#106974

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.