Somehow the return value of the function date() makes no sense here. Does anybody know why?
Maybe I am missing something but according to the PHP Manual the following doesn't make sense:
echo date('W', strtotime("2021-01-01"));
output:
53
Shouldn't the first day of the year be in week 01? Other dates seem to be broken as well (e.g. 2021-01-05 returning 01 ).
I checked the output of strtotime() but the UNIX timestamp seems to be correct. What am I missing here?
PHP's DateTime format doc:
W - ISO-8601 week number of year, weeks starting on Monday
ISO-8601 definition of first week of the year:
Weeks start with Monday and end on Sunday. Each week's year is the Gregorian year in which the Thursday falls. The first week of the year, hence, always contains 4 January.
it return 53 because 01/01/2021 is in week 53 of year 2020.
If you do
echo date('W', strtotime("2021-01-04")); // first Monday of year 2021
it will return 01
Shouldn't the first day of the year be in week 01?
Like @catcon said, no.
To answer your comment,
This is correct but do you have an example of how to get the correct week number for the calendar year? In this case 1 for 2021-01-01
z = The day of the year (starting from 0) 0 through 365
From there, get the day of the year, subtract from 365 or 366, divide by 7, subtract from weeks in a year (365/7).