简体   繁体   English

使用时区将DateTime转换为ISO 8601 DateTime格式

[英]Convert DateTime to ISO 8601 DateTime Format with timezone

How do I convert my time from 2010-12-30 23:21:46 to ISO 8601 date format? 如何将我从2010-12-30 23:21:46的时间转换为ISO 8601日期格式?

I tried $datetime->format(DateTime::ATOM) and date(DATE_ISO8601, strtotime('2010-12-30 23:21:46')) which converts into ISO8601 format like 2016-12-31T09:47:50+0000 . 我尝试了$datetime->format(DateTime::ATOM)date(DATE_ISO8601, strtotime('2010-12-30 23:21:46'))将其转换为ISO8601格式,例如2016-12-31T09:47:50+0000 But what i need is 2018-02-19T05:43:59.753000Z format. 但是我需要的是2018-02-19T05:43:59.753000Z格式。 How can i achieve using PHP?? 如何使用PHP实现??

If you wish to output format in Zulu/UTC/GMT timezone, then you must first convert timezone, since I do not know in which timezone is your current setup. 如果您希望以Zulu / UTC / GMT时区输出格式,则必须先转换时区,因为我不知道您当前的设置在哪个时区。

$dt = new DateTime('2010-12-30 23:21:46');
$dt->setTimezone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d\TH:i:s.u\Z');

demo 演示


And ISO-8601 is a standard that has many variations of format: ISO-8601是一种具有多种格式变化的标准:

  • 2018-04-03
  • 2018-04-03T05:20:03+00:00
  • 2018-04-03T05:20:03Z
  • 20180403T052003Z
  • 2018-W14
  • 2018-W14-2
  • ... ...

This are all valid ISO-8601 formats. 这些都是有效的ISO-8601格式。 Read more here . 在这里阅读更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM