note 52263 added to function.strtotime
| From: | EdLecky-Thompson at osu1 dot php dot net | Date: | Tue, 26 Apr 2005 16:04:01 +0000 |
| Subject: | note 52263 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88569@lists.php.net to get a copy of this message | ||
Here's a quick function which can replace strtotime, and will work fine on dates pre-1970 (i.e.
it will return a negative number as expected).
This negative time stamp seems to be supported as an input parameter by methods like date() up to a
point, but if you get crazy and start talking about dates in the 1700s (everybody was using PHP3
back then, of course) it gets upset.
For those of you doing staff databases and so forth, of course, this is probably fine - it's
definitely OK for any dates post 1900, and this value has been hard coded into the function below.
function safestrtotime($strInput) {
$iVal = -1;
for ($i=1900; $i<=1969; $i++) {
# Check for this year string in date
$strYear = (string)$i;
if (!(strpos($strInput, $strYear)===false)) {
$replYear = $strYear;
$yearSkew = 1970 - $i;
$strInput = str_replace($strYear, "1970", $strInput);
};
};
$iVal = strtotime($strInput);
if ($yearSkew > 0) {
$numSecs = (60 * 60 * 24 * 365 * $yearSkew);
$iVal = $iVal - $numSecs;
$numLeapYears = 0; # Work out number of leap years in period
for ($j=$replYear; $j<=1969; $j++) {
$thisYear = $j;
$isLeapYear = false;
# Is div by 4?
if (($thisYear % 4) == 0) {
$isLeapYear = true;
};
# Is div by 100?
if (($thisYear % 100) == 0) {
$isLeapYear = false;
};
# Is div by 1000?
if (($thisYear % 1000) == 0) {
$isLeapYear = true;
};
if ($isLeapYear == true) {
$numLeapYears++;
};
};
$iVal = $iVal - (60 * 60 * 24 * $numLeapYears);
};
return($iVal);
};
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52263
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52263&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52263&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52263&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52263&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52263&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52263&report=yes
Search -- http://master.php.net/manage/user-notes.php