PHP Dates Issue – Getting the correct week number-ThrowExceptions

Exception or error:

Currently, I’m facing an issue with getting the correct week number.
It works correctly up until the last 2 days of 2019 where it returns the first week of this year.

 $weak_number = date('W' , strtotime("2019-12-31")); // returns 01 of 2020

How can I get around that?

How to solve:

Thank you for interesting information.

I didn’t know this before, but from wiki article:

Last week

The last week of the ISO week-numbering year, i.e. W52 or W53, is the week before W01 of the next year. This week’s properties are:

It has the year's last Thursday in it.
It is the last week with a majority (4 or more) of its days in December.
Its middle day, Thursday, falls in the ending year.
Its last day is the Sunday nearest to 31 December.
It has 28 December in it.
Hence the earliest possible last week extends from Monday 22 December to Sunday 28 December, the latest possible last week extends from Monday 28 December to Sunday 3 January.

If 31 December is on a Monday or Tuesday it is in W01 of the next year. If it is on a Wednesday, it is in W01 of the next year in common years and W53 in leap years. If it is on a Thursday, it is in W53 of the year just ending. If on a Friday or Saturday it is in W52 of the year just ending. If on a Sunday, it is in W52 of the year just ending in common years and W01 of the next year in leap years.

Here is the link: https://en.wikipedia.org/wiki/ISO_week_date

And some calendars with week number:

Leave a Reply

Your email address will not be published. Required fields are marked *