简体   繁体   English

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

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

I have checked out the following question/responses: How do I get the format of “yyyy-MM-ddTHH:mm:ss.fffZ” in php?我检查了以下问题/回复: 如何在 php 中获取“yyyy-MM-ddTHH:mm:ss.fffZ”的格式? The responses include links to Microsoft documentation to format dates but these do not work in PHP.响应包括 Microsoft 文档的链接以格式化日期,但这些在 PHP 中不起作用。

The the top answer suggest最佳答案建议

date('Ym-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.我正在使用 API,它需要上述格式的时间戳。 The API is based in the UK (GMT) and my server is in Australia (AEST). 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

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

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.不幸的是,当我发布这种格式时,API 正在读取时间并休息 10 个小时。 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).我收到一条错误消息,指出日期不能是过去的日期(因为它正在对照墨尔本的当地时间进行检查,但看到的时间早 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.我需要匹配所需的时间戳,但我找不到任何方法来复制上述 output,在澳大利亚墨尔本的 PHP。 Any assistance is much appreciated.非常感谢任何帮助。

First question on SO so please let me know how I have gone关于 SO 的第一个问题,所以请告诉我我的情况

Z stands for the timezone UTC and 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.\0\0\0\Z');

Note that I've zeroed the millisecond part and escaped the special characters T and Z in the format pattern. 请注意,我已将毫秒零件归零,并以格式模式转义特殊字符TZ

The 3 number last before Z is just the 3 decimal place of time in milliseconds Z之前的 3 数字只是时间的小数点后 3 位,以毫秒为单位

The function microtime(true) gave the current time in milliseconds, would output like 1631882476.298437 In this situation it would be .298Z function microtime(true)以毫秒为单位给出当前时间,output 就像1631882476.298437在这种情况下它将是.298Z

Examples例子

  • 1652030212.6311 =.631Z 1652030212.6311 =.631Z
  • 1652030348.0262 =.026Z 1652030348.0262 =.026Z
  • 1652030378.5458 =.545Z 1652030378.5458 =.545Z

Codes代码

$milliseconds = microtime(true);
    
// Round to integer 
$timestamp = floor($milliseconds);

// Get number after dots
$uuuu = preg_replace("/\d+\./", "", "$milliseconds");

// Get last 3 number decimal place
$u = substr($uuuu, 0, 3);

// Print date by the timestamp timestamp
echo date("Y-m-d\TH:i:s", $timestamp). ".{$u}Z";

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

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