php 如何格式化UTC日期以在php中使用Z(祖鲁语)区域指示符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17390784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:48:38  来源:igfitidea点击:

How to format an UTC date to use the Z (Zulu) zone designator in php?

phpdatetimeiso8601

提问by sallaigy

I need to display and handle UTC dates in the following format:

我需要按以下格式显示和处理 UTC 日期:

2013-06-28T22:15:00Z

2013-06-28T22:15:00Z

As this format is part of the ISO8601 standard I have no trouble creating DateTime objects from strings like the one above. However I can't find a clean way (meaning no string manipulations like substr and replace, etc.) to present my DateTime object in the desired format. I tried to tweak the server and php datetime settings, with little success. I always get:

由于此格式是 ISO8601 标准的一部分,因此我可以轻松地从上述字符串创建 DateTime 对象。但是我找不到一种干净的方法(意味着没有像 substr 和 replace 等字符串操作)来以所需的格式显示我的 DateTime 对象。我试图调整服务器和 php 日期时间设置,但收效甚微。我总是得到:

$date->format(DateTime::ISO8601); // gives 2013-06-28T22:15:00+00:00

Is there any date format or configuration setting that will give me the desired string? Or I'll have to append the 'Z' manually to a custom time format?

是否有任何日期格式或配置设置可以为我提供所需的字符串?或者我必须手动将“Z”附加到自定义时间格式?

回答by hek2mgl

No, there is no special constant for the desired format. I would use:

不,所需的格式没有特殊的常量。我会用:

$date->format('Y-m-d\TH:i:s\Z');

But you will have to make sure that the times you are using are really UTC to avoid interpretation errors in your application.

但是您必须确保您使用的时间确实是 UTC,以避免在您的应用程序中出现解释错误。

回答by Ynhockey

In order to get the UTC date in the desired format, you can use something like this:

为了以所需格式获取 UTC 日期,您可以使用以下内容:

gmdate('Y-m-d\TH:i:s\Z', $date->format('U'));

回答by Chris Seufert

To do this with the object-oriented style date object you need to first set the timezone to UTC, and then output the date:

要使用面向对象样式的日期对象执行此操作,您需要首先将时区设置为 UTC,然后输出日期:

function dateTo8601Zulu(\DateTimeInterface $date):string {
  return $date
    ->setTimezone(new \DateTimeZone('UTC'))
    ->format('Y-m-d\TH:i:s\Z');
}

回答by Edmund Sulzanok

If you are using Carbon then the method is:

如果您使用的是 Carbon,则方法是:

echo $dt->toIso8601ZuluString();    
// 2019-02-01T03:45:27Z

回答by Sergii Shymko

The Zulu time zone (Z) is exactly the same as Coordinated Universal Time (UTC). Interestingly, PHP recognizes "Z" as a valid timezone code although it's not listed among supported timezones.

祖鲁时区 (Z) 与协调世界时 (UTC) 完全相同。有趣的是,PHP 将“Z”识别为有效的时区代码,尽管它没有在受支持的时区中列出。

That allows to leverage the built-in format character"T" that renders the timezone abbreviation dynamically instead of the hard-coded "Z" suffix.

这允许利用动态呈现时区缩写的内置格式字符“T”而不是硬编码的“Z”后缀。

$zulu = $date->setTimeZone(new \DateTimeZone('Z'));
echo $zulu->format('Y-m-d\TH:i:sT');
// 2020-05-20T21:09:48Z

The example assumes \DateTimeImmutableinstance that gets cloned by the timezone setter while keeping the original instance intact.

该示例假设\DateTimeImmutable实例由时区设置器克隆,同时保持原始实例完好无损。

UPDATE:Initially, the answer featured the built-in constant \DateTime::RFC3339that relies on the format character "P" instead of the needed "T" making it not suitable. Admittedly, the solution has lost its appeal as it now requires to explicitly specify the format just like the alternatives.

更新:最初,答案的特点是内置常量\DateTime::RFC3339依赖于格式字符“P”而不是所需的“T”,因此它不适合。不可否认,该解决方案已经失去了吸引力,因为它现在需要像替代方案一样明确指定格式。