note 107354 added to function.strtotime
| From: | giacomo at cinquepunti dot it | Date: | Thu, 02 Feb 2012 16:09:26 +0000 |
| Subject: | note 107354 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185833@lists.php.net to get a copy of this message | ||
//this function allow the use of native strtotime with dates before 1970 (and before 1900) even in
Windows
function safe_strtotime($string)
{
if(!preg_match("/\d{4}/", $string, $match)) return null; //year must be in YYYY form
$year = intval($match[0]);//converting the year to integer
if($year >= 1970) return date("Y-m-d", strtotime($string));//the year is after 1970 -
no problems even for Windows
if(stristr(PHP_OS, "WIN") && !stristr(PHP_OS, "DARWIN")) //OS seems to
be Windows, not Unix nor Mac
{
$diff = 1975 - $year;//calculating the difference between 1975 and the year
$new_year = $year + $diff;//year + diff = new_year will be for sure > 1970
$new_date = date("Y-m-d", strtotime(str_replace($year, $new_year, $string)));//replacing
the year with the new_year, try strtotime, rendering the date
return str_replace($new_year, $year, $new_date);//returning the date with the correct year
}
return date("Y-m-d", strtotime($string));//do normal strtotime
}
----
Server IP: 69.147.83.197
Probable Submitter: 147.125.99.203
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- https://master.php.net/note/edit/107354
Del: integrated -- https://master.php.net/note/delete/107354/integrated
Del: useless -- https://master.php.net/note/delete/107354/useless
Del: bad code -- https://master.php.net/note/delete/107354/bad+code
Del: spam -- https://master.php.net/note/delete/107354/spam
Del: non-english -- https://master.php.net/note/delete/107354/non-english
Del: in docs -- https://master.php.net/note/delete/107354/in+docs
Del: other reasons-- https://master.php.net/note/delete/107354
Reject -- https://master.php.net/note/reject/107354
Search -- https://master.php.net/manage/user-notes.php