PHP date() 返回格式 yyyy-mm-ddThh:mm:ss.uZ

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24605384/
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 17:23:17  来源:igfitidea点击:

PHP date() returning format yyyy-mm-ddThh:mm:ss.uZ

phpdatedate-formatting

提问by Patrick

I have checked out the following question/responses: How do I get the format of “yyyy-MM-ddTHH:mm:ss.fffZ” in php?The responses include links to Microsoft documentation to format dates but these do not work in PHP.

我查看了以下问题/回复: 如何在 php 中获取“yyyy-MM-ddTHH:mm:ss.fffZ”的格式?响应包括指向 Microsoft 文档以格式化日期的链接,但这些在 PHP 中不起作用。

The the top answer suggest

最佳答案提示

date('Y-m-dTH:i:s.uZ') //for the current time

date('Y-m-dTH:i:s.uZ') //for the current time

This outputs 2013-03-22EDT12:56:35.000000-1440016

这输出 2013-03-22EDT12:56:35.000000-1440016

Background

背景

I am working with an API which requires a timestamp in the format above. The API is based in the UK (GMT) and my server is in Australia (AEST).

我正在使用一个需要上述格式的时间戳的 API。API 位于英国 (GMT),我的服务器位于澳大利亚 (AEST)。

The example given in the API documentation ask for the date to be in this format:

API 文档中给出的示例要求日期采用以下格式:

2011-07-15T16:10:45.555Z

2011-07-15T16:10:45.555Z

The closest I can get to this is date('c') which outputs:

我能得到的最接近的是 date('c') 输出:

2014-07-03T16:41:59+10:00//Notice the Z is replaced with a time diff in hours

2014-07-03T16:41:59+10:00//Notice the Z is replaced with a time diff in hours

I believe the 'Z' refers to a Zone but it is not mentioned in the PHP documentation.

我相信“Z”指的是一个区域,但 PHP 文档中没有提到它。

Unfortunatly when I post this format, the API is reading the time and taking 10 hours off. I get an error saying that the date cannot be in the past (as it is checking against the local time in Melbourne, but seeing a time 10 hours earlier).

不幸的是,当我发布此格式时,API 正在读取时间并请假 10 小时。我收到一条错误消息,说日期不能是过去的(因为它正在检查墨尔本的当地时间,但看到的时间早了 10 小时)。

I have tried trimming the timestamp to remove the +1000 which the API accepts, but the record is showing as created as 10 hours earlier.

我尝试修剪时间戳以删除 API 接受的 +1000,但记录显示为 10 小时前创建的。

I need to match the timestamp required but I cannot find any way to replicate the above output, in PHP for Melbourne, Australia. Any assistance is much appreciated.

我需要匹配所需的时间戳,但我找不到任何方法在澳大利亚墨尔本的 PHP 中复制上述输出。非常感谢任何帮助。

First question on SO so please let me know how I have gone

关于 SO 的第一个问题所以请让我知道我是怎么去的

采纳答案by hek2mgl

Zstands for the timezone UTCand is defined in ISO-8601, which is your desired output format, extended by the millisecond part.

Z代表时区UTC并在ISO-8601 中定义,这是您所需的输出格式,以毫秒部分扩展。

Before outputting the time, you'll need to transfer local times to UTC:

在输出时间之前,您需要将当地时间转换为 UTC:

$dt = new DateTime();
$dt->setTimeZone(new DateTimeZone('UTC'));

then you can use the following format string:

那么您可以使用以下格式字符串:

echo $d->format('Y-m-d\TH-i-s.##代码####代码####代码##\Z');

Note that I've zeroed the millisecond part and escaped the special characters Tand Zin the format pattern.

请注意,我已经将毫秒部分归零并转义了特殊字符TZ格式模式。