note 52738 added to function.strtotime

From: Date: Wed, 11 May 2005 12:34:41 +0000
Subject: note 52738 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-89256@lists.php.net to get a copy of this message
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. ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52738 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52738&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52738&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52738&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52738&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52738&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52738&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#89256) next »