将日期时间转换为带时区的 ISO 8601 日期时间格式

Convert DateTime to ISO 8601 DateTime Format with timezone

如何将我的时间从 2010-12-30 23:21:46 转换为 ISO 8601 日期格式?

我尝试了 $datetime->format(DateTime::ATOM)date(DATE_ISO8601, strtotime('2010-12-30 23:21:46')),它们转换成 ISO8601 格式,如 2016-12-31T09:47:50+0000。但我需要的是 2018-02-19T05:43:59.753000Z 格式。我如何使用 PHP 实现?

如果你想在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


而 ISO-8601 是一个有多种格式变化的标准:

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

这些都是有效的 ISO-8601 格式。阅读更多 here.