douyu4822 2013-10-16 06:35
浏览 78
已采纳

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

I was trying to convert a datetime to another format.

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

It is in m-d-Y H:i:s format and i want to convert it into Y-m-d H:i:s to save it to the database.

I've already tried the 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

How can i do this kind of thing. Thanks for the help.

  • 写回答

2条回答 默认 最新

  • dougang5993 2013-10-16 06:39
    关注

    See 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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?