php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35699 date() can't handle leap years before 1970
Submitted: 2005-12-16 06:11 UTC Modified: 2005-12-20 17:01 UTC
From: iquito at gmx dot net Assigned: derick
Status: Closed Package: Date/time related
PHP Version: 5.1.1 OS: Debian Sarge
Private report: No CVE-ID:
 [2005-12-16 06:11 UTC] iquito at gmx dot net
Description:
------------
strtotime() doesn't seem to handle leap years before 1970 correctly, all dates in a leap year are off by one day when converted by strtotime().

Reproduce code:
---------------
/* shows 1964-06-07 instead of 1964-06-06 */

echo date('Y-m-d', strtotime('1964-06-06'));

/* shows the correct date, 1963-06-06, thus confirming that it has something to do with leap years, non-leap-years aren't affected. */

echo date('Y-m-d', strtotime('1963-06-06'));

/* curiously, also dates in january and february of a leap year are affected by this bug */

echo date('Y-m-d', strtotime('1964-01-06')); // returns 1964-01-07

Expected result:
----------------
echo date('Y-m-d', strtotime('1964-06-06')); // should return 1964-06-06 !

Actual result:
--------------
echo date('Y-m-d', strtotime('1964-06-06')); // returns 1964-06-07 !

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-12-20 16:20 UTC] derick@php.net
strtotime is actually corrrect here, it's the date() function that is broken:

<?php
date_default_timezone_set("UTC");
$utc_time1 = strtotime('1964-01-01 00:00:00 UTC');
$utc_time2 = strtotime('1963-12-31 00:00:00 UTC');
echo $utc_time1, ':', $utc_time2, " - ", $utc_time1 - $utc_time2, "\n";
echo date(DATE_ISO8601, $utc_time1), "\n";
echo date(DATE_ISO8601, $utc_time2), "\n";
?>

-189388800:-189475200 - 86400
1964-01-02T00:00:00+0000
1963-12-31T00:00:00+0000

compare timestamp with date program:
derick@kossu:~$ date +%s --date "1964-01-01 00:00:00 UTC"
-189388800
derick@kossu:~$ date +%s --date "1963-12-31 00:00:00 UTC"
-189475200

 [2005-12-20 17:01 UTC] derick@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 13:02:46 2014 UTC