drn61317 2014-07-07 07:45
浏览 188
已采纳

PHP date()返回格式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? The responses include links to Microsoft documentation to format dates but these do not work in PHP.

The the top answer suggest

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

This outputs 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).

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

2011-07-15T16:10:45.555Z

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

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.

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).

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.

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.

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

  • 写回答

1条回答 默认 最新

  • dongtu1958 2014-07-07 07:58
    关注

    Z stands for the timezone UTC and is defined in ISO-8601, which is your desired output format, extended by the millisecond part.

    Before outputting the time, you'll need to transfer local times to 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了