I am trying to get the year and week of a date with format 'yyyy-mm-dd', for example '2021-01-01' and with this code I achieve it:
$date = new DateTime('2021-01-01');
$week = $date->format("W");
$year = reset(explode('-', '2021-01-01'));
And the result is week 53 of the year 2021, the problem is that this particular day belongs to week 53 of the year 2020, not to week 1 of the year 2021.
I need the result to be year 2020 week 53.
How could I make the exception in this case?
To get the year that corresponds to the week number you need to use the format character
o
Example: