Home » Date and Time » Date » Bug #6419
All timestamps with a length of 8, will generate a wrong date.
Details
| Submitted | 2006-01-05 13:23 UTC |
|---|---|
| From | info at rhalff dot com |
| Assigned | pajoye |
| Status | Bogus |
| Package | Date |
| PHP Version | 5.0.0 |
| OS | Debian |
| Roadmaps | (Not assigned) |
Comments
[2006-01-05 13:23 UTC] info at rhalff dot com
Description:
------------
All timestamps with a length of 8, will generate a wrong ates when a timestamp is passed to the constructor.
There is a format parameter for the setDate() function to fix this, but this isn't used in the Date constructor:
function Date($date = null)
{
$this->tz = Date_TimeZone::getDefault();
if (is_null($date)) {
$this->setDate(date("Y-m-d H:i:s"));
} elseif (is_a($date, 'Date')) {
$this->copy($date);
} else {
$this->setDate($date); << NO FORMAT PARAMETER
}
}
The code causing the bug is the regular expression in setDate():
preg_match('/^(\d{4})-?(\d{2})-?(\d{2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)?$/i', $date, $regs) && $format != DATE_FORMAT_UNIXTIME
I could fix the bug by adding && !is_numeric($date) here, but I'm not sure whether that will break other uses of the Date class. Probably the regexp should be fixed all together.
Test script:
---------------
require 'Date.php';
for($i=0; $i<50; $i++) {
$timestamp = str_pad(rand(1, 99999999), 8, '0');
$date = new Date($timestamp);
printf("%s | %s %s %s\n",
strftime('%Y %m %d', $timestamp), // correct time
$date->getYear(),
$date->getMonth(),
$date->getDay()
);
}
Expected result:
----------------
1971 03 20 | 1971 03 20
1971 05 17 | 1971 05 17
1971 11 25 | 1971 11 25
etc..
Actual result:
--------------
1971 03 20 | 3831 73 5
1971 05 17 | 4329 18 59
1971 11 25 | 5990 31 20
etc..
[2006-01-05 13:24 UTC] info at rhalff dot com
s/a wrong ates/wrong dates/