|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77243 Weekdays are calculated incorrectly for negative years
Submitted: 2018-12-05 14:32 UTC Modified: 2022-06-09 15:59 UTC
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: bjoern dot fischer at dezem dot de Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.2.12 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: bjoern dot fischer at dezem dot de
New email:
PHP Version: OS:


 [2018-12-05 14:32 UTC] bjoern dot fischer at dezem dot de
When we reach negative years 'date()' calulates the day of the week 'N' incorrectly.

Apparently one day too much is subtracted in this case.
So that for example the date '-1-W52-3' is displayed as '-1-W52-2' instead, when using 'date()'.

The issue can be easily reproduced here:

Test script:

$time=-62167046400; // 0000-01-03 0-01-1

var_dump(date('Y-m-d o-W-N', $time - 86400));
var_dump(date('Y-m-d o-W-N', $time - 86400 * 2));
var_dump(date('Y-m-d o-W-N', $time - 86400 * 3), 'The issue appeared here. Expected: 0000-01-02 -1-52-5');
var_dump(date('Y-m-d o-W-N', $time - 86400 * 4));
var_dump(date('Y-m-d o-W-N', $time - 86400 * 5));

Expected result:
The date should be formatted correctly and the displayed weekday should match the actual weekday for a timestamp.

Actual result:
The date is formatted incorrectly and the displayed weekday, for negative years, is one less than the actual weekday.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-12-05 15:41 UTC]
From the date page :

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer). However, before PHP 5.1.0 this range was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

You may need to use a more specific library to cope with BCE dates, and to do things like deal with the Julian to Gregorian calendar changes.
 [2020-10-17 13:52 UTC]
> Expected: 0000-01-02

There is no year zero in the Gregorian nor the Julian calendar,
though.  Consider to use ext/calendar for such purposes.
 [2022-05-13 16:10 UTC]
-Status: Open +Status: Verified
 [2022-05-13 16:10 UTC]
Dan and Christoph are both incorrect.

PHP implements the ISO8601 calendar, which is the Gregorian calendar with the year 0. It supports timestamps in a 64-bit range, which is well beyond 1900 (like 213 billion years or so).

This indeed looks like a bug.
 [2022-05-13 16:10 UTC]
-Assigned To: +Assigned To: derick
 [2022-06-17 08:37 UTC]
Automatic comment on behalf of derickr
Log: Fixed bug #77243 (Weekdays are calculated incorrectly for negative years)
 [2022-06-17 08:37 UTC]
-Status: Verified +Status: Closed
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 12 18:01:31 2024 UTC