如何在php中将ISO8601转换为日期格式

2022-08-30 15:08:20

如何转换(ISO8601格式):2014-03-13T09:05:50.240Z

为此(MySQL DATE格式):2014-03-13

在 php 中?


答案 1

试试这个

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime($date));

完整的日期函数文档可以在这里找到:http://php.net/manual/en/function.date.php

PHP函数“strtotime”不执行任何其他操作,然后将时间字符串转换为unix时间戳。

希望我能帮:)

P.S.:以防万一 strtotime 将返回 0,请尝试使用以下命令:

$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime(substr($date,0,10)));

答案 2

由于PHP,你也可以使用OOPDateTime()来做到这一点(当然,如果你更喜欢OOP):5.2.0

$now = new DateTime("2014-03-13T09:05:50.240Z");
echo $now->format('Y-m-d');    // MySQL datetime format

推荐