I have been relying very heavily on strtotime() in PHP in just about everything I write. For those not entirely familiar with this function, strtotime() will “parse about any English textual
Why create Unix date() date('m/d/Y'); will generate a reasonably nice US representation of today’s date. However, if you did date('m/d/Y',strtotime('3 weeks ago')); you can get the nice readable format provided by date, but for some arbitrary date you are deciding upon with strtotime. Also, knowing strtotime happily accepts the date('m/d/Y',strtotime($row['date_field']));. For a complete list of the available date formats, visit
Ok, so the title of this makes reference to a limitation… on to that. On some
In the meantime, safestrtotime() function and shared it on PHP.net. I’m reproducing it here for my convenience, altered to match the code formatting I prefer.
[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; // determine 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;
}
?>
[/php]
Pingback: NoSheep! » Disclaimer for Plymouth State Readers and Concerned Citizens
wow! this function was super helpful. thanks so much.
Hi! Very nice site! Thanks you very much! M3MsRBa2Am6K
it gives error like this
Undefined variable: yearSkew in c:\inetpub\wwwroot\qcadmin\Untitled-1.php on line 18
That’s a PHP strict error, I have not initialized all variables in this script to be strict mode compatible…
Pingback: Vicodin no prescription.
Hi,
I am getting the date like this using safestrtotime() function,
Input : 1/1/1900
call: strftime(“%Y-%m-%d”,$util->safestrtotime(’1/1/1900′))
Output date : 1901-12-14
ДнепропетровÑк
необычные кольца на заказ
Блюда Ð´Ð»Ñ Ð´ÐµÑ‚ÐµÐ¹
отель в моÑкве
двери моÑква
You need to insert a line between line 5 and 6:
$yearSkew = 0;
If no year skew is needed, the posted code will throw a E_NOTICE error.
(Notice errors are displayed when turned off; you will find the display of these errors turned off a lot of the time, which is why might not have seen it.)
Thanks so much for pointing that out!