dongliang1941 2019-08-05 01:54
浏览 110
已采纳

使用php修改日期时间格式

I have to import some data from a csv file where the dates have been stored in the following format:

15/07/2019 11:16:13

I need to convert this to

2019-07-15 11:16:13

I tried

$time = strtotime($column [2]);
$column_2 = date('Y-m-d H:i:s',$time);

where $column [2] is the csv column where this data is held, but it just returned a date of 1970-01-01 01:00:00

When I echo $time it is empty, so I'm guessing strtotime doesn't work on the date time format I'm trying to use.

What is the correct method for doing this?

  • 写回答

1条回答 默认 最新

  • douyi8760 2019-08-05 02:07
    关注

    You can use DateTime::createFromFormat()

    $time = DateTime::createFromFormat('d/m/Y H:i:s', $column[2]);
    $column_2 = $time->format('Y-m-d H:i:s');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?