【发布时间】:2021-09-13 11:56:39
【问题描述】:
例如
strtotime("2018-12-06T09:04:55");
strtotime("2021-07-09T14:09:47.529751-04:00");
我在 php 手册中读到使用 strtotime 时应避免使用 ISO 日期,为什么? 我应该在使用 strtotime 之前从字符串中提取日期时间吗?
strtotime() 将转换没有时区指示的字符串,就好像该字符串是默认时区 ( date_default_timezone_set() ) 中的时间。因此,使用 strtotime() 转换像 '2018-12-06T09:04:55' 这样的 UTC 时间实际上会产生错误的结果。在这种情况下使用:
<?php
function UTCdatestringToTime($utcdatestring)
{
$tz = date_default_timezone_get();
date_default_timezone_set('UTC');
$result = strtotime($utcdatestring);
date_default_timezone_set($tz);
return $result;
}
?>
【问题讨论】:
标签: php date time unix-timestamp strtotime