我正在刮胡子,我得到了一个类似23 Nov, 2015 16:44:26 GMT
的约会。我想把它转换成Y-m-d H:i:s
,这样我就可以把它保存到数据库日期时间字段中。
我试过这样:
echo date("Y-m-d H:i:s", strtotime("23 Nov, 2015 16:44:26 GMT"));
我还删除了preg_replace
和的GMT
echo date("Y-m-d H:i:s", strtotime("23 Nov, 2015 16:44:26"));
它给出1970-01-01 01:00:00
您可以使用DateTime::createFromFormat
将字符串转换为DateTime对象。http://php.net/manual/en/datetime.createfromformat.php
您可以使用DateTime::createFromFormat创建DateTime。
对于日期中的"GMT"部分,您可以在时区缩写的格式中使用"T"。
$dateTime = DateTime::createFromFormat('d M, Y H:i:s T', '23 Nov, 2015 16:44:26 GMT');
echo $dateTime->format('Y-m-d H:i:s');
将导致:
2015-11-23 16:44:26
演示
尝试这个
<?php
$datestr = "23 Nov, 2015 16:44:26 GMT";
$datestr = str_replace(",", "", $datestr);
$dateArray = explode(" ", $datestr) ;
unset($dateArray[count($dateArray)-1]);
$newDateStr = '';
$i=0;
foreach ($dateArray as $dateArrayValue)
{
$hyphenStr = " ";
if($i>0 && $i != 3)
{
$hyphenStr = "-";
}
$newDateStr .= $hyphenStr.$dateArrayValue ;
$i++;
}
$newDateStr = trim($newDateStr);
echo date("Y-m-d H:i:s", strtotime($newDateStr));
?>
另请访问:http://php.net/manual/en/function.strtotime.php