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

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊