Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? 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

like image 651
Patrick Avatar asked Jul 07 '14 07:07

Patrick


People also ask

What is T and Z in timestamp PHP?

E.g: 2011-08-12T20:17:46.384Z. The T doesn't really stand for anything. It is just the separator that the ISO 8601 combined date-time format requires. You can read it as an abbreviation for Time. The Z stands for the Zero timezone, as it is offset by 0 from the Coordinated Universal Time (UTC).

How can I get current date in yyyy-mm-dd format in PHP?

date_default_timezone_set('UTC'); echo "<strong>Display current date dd/mm/yyyy format </strong>". "<br />"; echo date("d/m/Y"). "<br />"; echo "<strong>Display current date mm/dd/yyyy format</strong> "."<br />"; echo date("m/d/Y")."<br />"; echo "<strong>Display current date mm-dd-yyyy format </strong>".

What date is yyyy mm DDThh mm SSZ?

"YYYY-MM-DDThh:mm:ss-hh:mm" is ISO 8601 format.

How to convert mm dd yyyy to yyyy-mm-dd in PHP?

Convert DD-MM-YYYY to YYYY/MM/DD Here is how to do that: <? php $orgDate = "15-05-2020"; $date = str_replace('-"', '/', $orgDate); $newDate = date("Y/m/d", strtotime($date)); echo "New date format is: " .


1 Answers

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.

like image 68
hek2mgl Avatar answered Sep 26 '22 08:09

hek2mgl