note 52262 modified in function.strpos by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 16:40:30 +0000 |
| Subject: | note 52262 modified in function.strpos by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-154875@lists.php.net to get a copy of this message | ||
Here's a quick function which can [compliment] 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.
<?php
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);
};
?>
--was--
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);
};
http://php.net/manual/en/function.strpos.php