PHP 日期格式,如“2015 年 11 月 23 日 16:44:26 GMT”

PHP date format from a date format like "23 Nov, 2015 16:44:26 GMT"

我正在抓取,我得到了一个像 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

试试这个

<?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

您可以创建 DateTime using DateTime::createFromFormat.

对于日期的 'GMT' 部分,您可以在 format 中使用 '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

Demo