|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-08-02 03:27 UTC] paul at stunning-stuff dot com
Description:
------------
Hi,
While I was writing my own getWeekNumber method I discovered gmdate('W') and date('W') both return incorrect results for the year 1992 and 2020. Both years end on a thursday and both are leap years. I'm guessing that this is exactly the reason they return incorrect results :).
Expected result:
The last 4 days in 1992 and 2020 belong to week 53 of those years according to ISO 8601 rules.
Actual result:
gmdate('W') and date('W') think the last 4 days of 1992 and 2020 belong to week 1 of the following year.
I only had the time to test these two years, but I expect that PHP will return the incorrect week number for every leap year that ends with a thursday.
Thanks,
Paul van der Maas
---
www.stunning-stuff.com
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 20:00:01 2025 UTC |
Here is some example code to reproduce the problem: <?php $timestamp = gmmktime(12, 0, 0, 12, 28, 1992); echo 'Week number for ' . gmdate('D M d, Y H:i:s', $timestamp) . ': ' . gmdate('W', $timestamp) . '<br>'; $timestamp = gmmktime(12, 0, 0, 12, 29, 1992); echo 'Week number for ' . gmdate('D M d, Y H:i:s', $timestamp) . ': ' . gmdate('W', $timestamp) . '<br>'; $timestamp = gmmktime(12, 0, 0, 12, 30, 1992); echo 'Week number for ' . gmdate('D M d, Y H:i:s', $timestamp) . ': ' . gmdate('W', $timestamp) . '<br>'; $timestamp = gmmktime(12, 0, 0, 12, 31, 1992); echo 'Week number for ' . gmdate('D M d, Y H:i:s', $timestamp) . ': ' . gmdate('W', $timestamp) . '<br>'; $timestamp = gmmktime(12, 0, 0, 12, 28, 2020); echo 'Week number for ' . gmdate('D M d, Y H:i:s', $timestamp) . ': ' . gmdate('W', $timestamp) . '<br>'; $timestamp = gmmktime(12, 0, 0, 12, 29, 2020); echo 'Week number for ' . gmdate('D M d, Y H:i:s', $timestamp) . ': ' . gmdate('W', $timestamp) . '<br>'; $timestamp = gmmktime(12, 0, 0, 12, 30, 2020); echo 'Week number for ' . gmdate('D M d, Y H:i:s', $timestamp) . ': ' . gmdate('W', $timestamp) . '<br>'; $timestamp = gmmktime(12, 0, 0, 12, 31, 2020); echo 'Week number for ' . gmdate('D M d, Y H:i:s', $timestamp) . ': ' . gmdate('W', $timestamp) . '<br>'; ?> Expected result: Week number for Mon Dec 28, 1992 12:00:00: 53 Week number for Tue Dec 29, 1992 12:00:00: 53 Week number for Wed Dec 30, 1992 12:00:00: 53 Week number for Thu Dec 31, 1992 12:00:00: 53 Week number for Mon Dec 28, 2020 12:00:00: 53 Week number for Tue Dec 29, 2020 12:00:00: 53 Week number for Wed Dec 30, 2020 12:00:00: 53 Week number for Thu Dec 31, 2020 12:00:00: 53 Actual result: Week number for Mon Dec 28, 1992 12:00:00: 1 Week number for Tue Dec 29, 1992 12:00:00: 1 Week number for Wed Dec 30, 1992 12:00:00: 1 Week number for Thu Dec 31, 1992 12:00:00: 1 Week number for Mon Dec 28, 2020 12:00:00: 1 Week number for Tue Dec 29, 2020 12:00:00: 1 Week number for Wed Dec 30, 2020 12:00:00: 1 Week number for Thu Dec 31, 2020 12:00:00: 1 Check http://www.timeanddate.com/calendar/custom.html?year=1992&month=12&typ=1&display=1&wno=1 and http://www.timeanddate.com/calendar/custom.html?year=2020&month=12&typ=1&display=1&wno=1 to see these expected results confirmed. Sorry for the bulky post, but this way there is no room for misinterpretation. Thanks, Paul van der Maas --- www.stunning-stuff.comdate('W', $timestamp) fails to return "01" for some January 1st years on PHP version 5.3.2 and 5.2.8 on CentOS and Windows. $year = 1970; $month = 1; $day = 1; while ($year <= 2028) { $timestamp = mktime(12, 0, 0, $month, $day, $year); print $year . " :: " . date('W', $timestamp). " :: " . date('D', $timestamp) . "\n</br>"; $year++; } Expect 01 for every year but instead get 1970 :: 01 :: Thu 1971 :: 53 :: Fri 1972 :: 52 :: Sat 1973 :: 01 :: Mon 1974 :: 01 :: Tue 1975 :: 01 :: Wed 1976 :: 01 :: Thu 1977 :: 53 :: Sat 1978 :: 52 :: Sun ... 2020 :: 01 :: Wed 2021 :: 53 :: Fri 2022 :: 52 :: Sat 2023 :: 52 :: Sun 2024 :: 01 :: Mon 2025 :: 01 :: Wed 2026 :: 01 :: Thu 2027 :: 53 :: Fri 2028 :: 52 :: Sat 1st falling on Friday returns 53 1st falling on Saturday/Sunday return 52 Checked dates using http://www.tuxgraphics.org/toolbox/cal_year.html Warwick ShawHi, I have the same problem using date('W') to show week number of 13/June/2010 which should be 25, but it displays 23. I use php 5.2.6 through WAMP2. This is the first time I hear about snapshot so not sure what am I suppose to do to fix this problem. Can someone please explain to me what should I do? Thank you, SigalHello, it seems that this bug is still alive:-). My tries to calculate the week for timestamps belonging to 31 December 2012 are returning the wrong result as can be seen here: date('Y/W', 1356908400) => 2012/01 date('Y/m/d H:i:s', 1356908400) => 2012/12/31 00:00:00 Calls with timestamps belonging to 30/12/12 or 01/01/13 result in the correct output. Expected behavior (for me reading the ISO 8601 doc) would be to have the year 2013 in the first call to the date function. I'm using debian wheezy (testing) with the following php version: PHP 5.4.4-13 (cli) (built: Feb 19 2013 10:54:11) Thanks and cheers, Angel Tchorbadjiiski