note 52738 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 15 Jul 2007 12:48:26 +0000 |
| Subject: | note 52738 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-128583@lists.php.net to get a copy of this message | ||
Note Submitter: not provided
----
A shorter function for recognising dates before 1970 and returning a negative number is below. All
it does is replaces years before 1970 with ones 68 years later (1904 becomes 1972), and then
offsets the return value by a couple billion seconds. It works back to 1/1/1902, but only on dates
that have a century.
function safestrtotime ($s) {
$basetime = 0;
if (preg_match ("/19(\d\d)/", $s, $m) && ($m[1] < 70)) {
$s = preg_replace ("/19\d\d/", 1900 + $m[1]+68, $s);
$basetime = 0x80000000 + 1570448;
}
return $basetime + strtotime ($s);
}
Note that a negative number is stored the same as a really big positive number. 0x80000000 is the
number of seconds between 13/12/1901 20:45:54 and 1/1/1970 00:00:00. And 1570448 is the seconds
between this date and 1/1/1902 00:00:00, which is 68 years before 1/1/1970.