Bug #1199: mktime() doesn't correctly trap invalid dates
| From: | douw at vis dot co dot za | Date: | Wed, 03 Mar 1999 08:07:44 +0000 |
| Subject: | Bug #1199: mktime() doesn't correctly trap invalid dates | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-4455@lists.php.net to get a copy of this message | ||
From: douw@vis.co.za
Operating system: Unix
PHP version: 3.0.6
PHP Bug Type: Misbehaving function
Bug description: mktime() doesn't correctly trap invalid dates
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 {
...
}
--
PHP Development Mailing List http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe@lists.php.net
For help: php-dev-help@lists.php.net