[英]Convert Time to ISO 8601 format in UTC timezone
我的日期是Fri, 15 Mar 2019 08:56:57 +0000
我想在 php 的 UTC 时区将此日期转换为 ISO 8601 格式。
我尝试了以下方法:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
$output = $date->format(\DateTime::ATOM);
这个对吗?
如果你想要 ISO 8601 格式,你应该在 format 方法中使用DateTimeInterface::ISO8601
,或者你可以使用"c"
:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
echo $date->format(\DateTime::ISO8601);
// will be 2019-03-15T08:56:57+0000
echo $date->format("c");
/*
will be 2019-03-15T08:56:57+00:00
note the : in between hh and mm in offset(timezone)
generally accepted as valid ISO 8061 see:
https://en.wikipedia.org/wiki/ISO_8601#Time_offsets_from_UTC
*/
关于时区,如果你想强制它进入 UCT 时区,那么你应该首先在日期 object 上使用setTimezone
方法,时区参数为"UTC"
:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0000");
$date->setTimezone(new DateTimeZone("UTC"));
$output = $date->format(\DateTime::ISO8601);
请注意,如果您的原始日期时间不是 UTC(有偏移量),则时间将转换为 UTC,偏移量将为 0000:
$date = new DateTime("Fri, 15 Mar 2019 08:56:57 +0230");
echo $date->format(\DateTime::ISO8601);
// will be: 2019-03-15T08:56:57+0230
$date->setTimezone(new DateTimeZone("UTC"));
echo $date->format(\DateTime::ISO8601);
// will be: 2019-03-15T06:26:57+0000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.