php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #1199 mktime() doesn't correctly trap invalid dates
Submitted: 1999-03-03 03:07 UTC Modified: 1999-06-01 15:52 UTC
From: douw at vis dot co dot za Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.6 OS: Unix
Private report: No CVE-ID: None
 [1999-03-03 03:07 UTC] douw at vis dot co dot za
mktime() appears to use the C mktime() function which takes years since 1900
hence the  code line
   ta.tm_year = arguments[5]->value.lval - ((arguments[5]->value.lval > 1000) ? 
   1900 : 0);

So the function should, I think, work correctly for dates in the ranges 70-137 and 1970-2037, and return -1 for other years. However, as the following code fragment shows, many other invalid dates are not detected and return incorrect values.

for ($i = -100; $i < 32767; $i++) {
   $imod = $i - (($i > 1000) ? 1900 : 0);
   $timestamp = mktime(12, 0, 0, 1, 20, $i);
   if ($timestamp != -1)
      echo "$i, $imod => $timestamp => ".date("d M Y", $timestamp)."<br>\n";
}

Would it not be best to put explicit date range tests into the function?
In fact, shouldn't the function require 4 digit years and have a structure like
   if (year < 1970 || year > 2037) {
      return -1;
   } else {
      ...
   }

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-06-01 15:52 UTC] jim at cvs dot php dot net
The lack of checking is intentional, as it allows
use in some date calculations. There is a checkdate()
function that does checking.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Mon Jul 06 08:00:01 2026 UTC