我有不同类型的日期字符串,下面给出了示例。如何在PHP中将这些字符串转换为日期时间。你能帮我解决这个问题吗?
示例:
1: 10:12 am ETAug 31, 2009
2: August 31, 2009, 9:08 AM ET
3: Sept. 1, 2009 10:20 a.m. ET
4: 9:45 am ETSep 2, 2009
我尝试了一些代码,但结果是错误的。
<?php
date = 'Aug. 28, 2009 10:26 a.m. ET';
$remove[] = "'";
$remove[] = '.';
$remove[] = ',';
$remove[] = 'am';
$remove[] = 'ET';
$replace = str_replace($remove, '', $date);
$space[] = ' ';
$datetime = str_replace($space, '-', $replace);
echo $datetime . '<br>';
$date = date_create_from_format('F-d-Y', $datetime);
echo "Format:" . $date->format('Y-m-d') . "\n";
?>
答案 0 :(得分:0)
我用下面的方法得到了答案。
$replacechar = str_replace($remove, '', $date);
$space[] = ' ';
$replacespace = str_replace($space, '-', trim($replacechar));
$explodeDate = explode('-', $replacespace);
$datetimeexplode = $explodeDate[0].'-'.$explodeDate[1].'-'.$explodeDate[2].' '.$explodeDate[3];
if(strstr($explodeDate[3], ":")){
$date_time = date_create_from_format('F-d-Y H:i', $datetimeexplode);
$dateTimeReplace = $date_time->format('Y-m-d H:i');
echo $dateTimeReplace;
}
$datetimeexplodesecond = $explodeDate[2].'-'.$explodeDate[3].'-'.$explodeDate[4].' '.$explodeDate[0];
if(strstr($explodeDate[0], ":")){
$date_time = date_create_from_format('F-d-Y H:i', $datetimeexplodesecond);
$dateTimeReplace = $date_time->format('Y-m-d H:i');
echo $dateTimeReplace;
}
答案是:
2009-8-31 10:12
2009-8-31 9:08
2009-9-1 10:20
2009-9-2 9:45