PHP: Determine Week Number
Tutorial by Stefan Trost | Last update on 2023-06-03 | Created on 2016-06-09
Today, I would like to show you how to retrieve the calender week (week number) of an arbitrary given date with PHP.
This is much easier than you might suspect: We only need the PHP function date() and the formatting character "W" for it. When passing this character as a first parameter and any date as a second parameter to the date() function, we get the calendar week respectively the week number of the year for our date as a result.
Here is an example for such a call:
$date = time(); // current date echo date("W", $date); // output, for example: 03 or 10
According to ISO-8601, for PHP, the week begins at Monday and the output is formatted in the form of two digits as a string (so, for example "01" instead of "1").
If we want to have a single digit number for the week numbers 1 to 9, we can just write (int)date("W", $date) to get it. With this, the result of the function is converted from a string to an integer number whose output is naturally single-digit without any leading zeros. We can of course also proceed in the same way if we need the calendar week not as a string but as a number (for example to calculate with it).
Year of the Calendar Week
There is one problem that arises almost at each turn of the year. The problem can occur whenever the first calendar week of the new year is already starting in December or if the last calendar week of a year is going over into January. In both of this cases, the usage of date("W Y", $date) leads to wrong results:
$date = strtotime('2027-01-01'); echo date("W Y", $date); // output: 53 2027
This example shows, what is going wrong: For the date 2027-01-01, we get the week number 53 of 2027. The reason for that wrong result is that "Y" is providing us the year of the passed date - this is 2027. However, we need 2026, because from the point of view of the calendar week, January, 1 is belonging to the last week of the last year in this case (this case can occur not only on the first of January but also for an 2nd or 3rd of January).
Fortunately, the developers of PHP have thought of this special case and bestowed the parameter "o" (lowercase O) upon us. If we use this parameter instead of the usual parameter Y for the year with the date() function, we will receive the year suitable for the passed date's week number. Here is an example:
$date = strtotime('2027-01-01'); echo date("W o", $date); // output: 53 2026
Using this parameter, the result is correct: We get the 53th calendar week of 2026, which ends in calendar year 2027 that year.
Of course, this code also works for the opposite case, that is in the event that the first calendar week of the new year begins on December 29th, 30th or 31st.
About the Author
You can find Software by Stefan Trost on sttmedia.com. Do you need an individual software solution according to your needs? - sttmedia.com/contact
Show Profile
Related Topics
JavaScript: Get current Date and Time
Tutorial | 1 Comment
MySQL: Write current Date or Time into Column
Tutorial | 0 Comments
PHP: Current Date and Time
Tutorial | 0 Comments
PHP: Wrong Year for first Calendar Week
Question | 1 Answer
VirtualBox: Change Date and Time
Tutorial | 10 Comments
Delphi/Lazarus: Display current Date and Time
Tip | 0 Comments
Change Date of File
Tutorial | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.