如何格式化UTC日期以在php中使用Z(祖鲁)区域指示符? [英] How to format an UTC date to use the Z (Zulu) zone designator in php?

查看:86
本文介绍了如何格式化UTC日期以在php中使用Z(祖鲁)区域指示符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


2013-06-28T22:15:00Z

2013-06-28T22:15:00Z

由于这种格式是ISO8601标准的一部分,我从上述字符串创建DateTime对象没有任何麻烦。然而,我找不到一个干净的方式(意味着没有字符串操作,如substr和replace等)以所需的格式显示我的DateTime对象。我试图调整服务器和php datetime设置,几乎没有成功。我总是得到:

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:

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

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

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?

推荐答案

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

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

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

但是,您必须确保您正在使用的时间是UTC,以避免解释错误您的申请。

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

这篇关于如何格式化UTC日期以在php中使用Z(祖鲁)区域指示符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆