datetime - php strtotime() function returns blank/0 -
i have datetime data in string format this:
sat mar 24 23:59:59 gmt 2012
i want convert utc timestamp, when try follows:
function texttotime($texttime) { if(!$texttime || $texttime=="") return null; // sat mar 24 23:59:59 gmt 2012 $bits = preg_split('/\s/', $texttime); // mar 24 2012 23:59:59 gmt return strtotime("$bits[1] $bits[2] $bits[5] $bits[3] bits[4]"); }
it outputs 0 (not null).
if change last line to:
// mar 24 2012 23:59:59 return strtotime("$bits[1] $bits[2] $bits[5] $bits[3]");
it outputs (but wrong timestamp, off -4 hours or so).
not quite sure why you're re-organising existing string, as...
echo $timestamp = strtotime('sat mar 24 23:59:59 gmt 2012');
...works correctly. (it returns 1332633599, can check via date('r', 1332633599);
(this result in "sat, 24 mar 2012 23:59:59 +0000", well.)
that said, if you're going extract of components of string, might use mktime. example:
function texttotime($texttime) { if(!$texttime || $texttime=="") return null; list($junk, $month, $day, $time, $timezone, $year) = explode(' ', $texttime); list($hour, $minute, $second) = explode(':', $time); return mktime($hour, $minute, $second, $month, $day, $year); }
Comments
Post a Comment