可以用这种格式转换我的字符串" A H:i m / d / y"到php中的DateTime?
示例字符串:AM 05:28 07/08/13
这项工作
$date = DateTime::createFromFormat('A H:i m/d/y', 'AM 05:28 07/08/13');
这有效:
$date = DateTime::createFromFormat('H:i m/d/y', '05:28 07/08/13');
$date = DateTime::createFromFormat('H:i m/d/y A', '05:28 07/08/13 AM');
丑陋的工作:
$myDate = 'AM 05:28 07/08/13';
$myDate = substr($myDate, 3, strlen($myDate)-3)." ".substr($myDate, 0, 2);
$date = DateTime::createFromFormat('H:i m/d/y A', $myDate);
答案 0 :(得分:2)
这是一个php bug。尝试使用下一个功能:
function createFromFormat($format, $time)
{
$is_pm = (stripos($time, 'PM') !== false);
$time = str_replace(array('AM', 'PM'), '', $time);
$format = str_replace('A', '', $format);
$date = DateTime::createFromFormat(trim($format), trim($time));
if ($is_pm)
{
$date->modify('+12 hours');
}
return $date;
}
$date = createFromFormat('H:i m/d/y A', '05:28 07/08/13 AM');
var_dump($date->format('d.m.Y H:i')); // string(16) "08.07.2013 05:28"
$date = createFromFormat('H:i m/d/y A', '05:28 07/08/13 PM');
var_dump($date->format('d.m.Y H:i')); // string(16) "08.07.2013 17:28"