日期时间转换为 PHP 中的其他格式


datetime conversion to other format in php

我试图将日期时间转换为另一种格式。

 $original_value = "10-30-2013 14:19:12";

它是m-d-Y H:i:s格式,我想将其转换为Y-m-d H:i:s以将其保存到数据库中。

我已经尝试过 ff:

 $original_value = "10-30-2013 14:19:12";
 $date = new DateTime(strtotime($original_value));
 echo $date->format('Y-m-d H:i:s');
 output: 2013-10-16 14:31:19
 // which is wrong since it gets the current datetime value

 $original_value = "10-30-2013 14:19:12";
 echo date('Y-m-d H:i:s', strtotime($original_value))
 output: 1970-01-01 08:00:00
 // which is also invalid

我怎么能做这种事情。感谢您的帮助。

请参阅DateTime::createFromFormat()

$original_value = '10-30-2013 14:19:12';
$date = DateTime::createFromFormat('m-d-Y H:i:s', $original_value);
echo $date->format('Y-m-d H:i:s');

我的方式是提前将年月日和时间分开。然后根据标准时间格式重新格式化。我的代码如下:

echo "<br>";
echo $original_value = "10-30-2013 14:19:12";
echo "<br>";
$tmp_array=explode("-",$original_value);
echo $month=$tmp_array[0];
echo "<br>";
echo $day=$tmp_array[1];
echo "<br>";
$tmp_array=explode(" ",$tmp_array[2]);
echo $year=$tmp_array[0];
echo "<br>";
echo $time=$tmp_array[1];
echo "<br>";
echo date('Y-m-d H:i:s', strtotime($year."-".$month."-".$day." ".$time));