Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format an UTC date to use the Z (Zulu) zone designator in php?

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

2013-06-28T22:15:00Z

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 

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?

like image 290
sallaigy Avatar asked Jun 30 '13 13:06

sallaigy


People also ask

What is Z in UTC format?

Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "T0930Z". "14:45:15 UTC" would be "14:45:15Z" or "T144515Z". The Z suffix in the ISO 8601 time representation is sometimes referred to as "Zulu time" because the same letter is used to designate the Zulu time zone.

What is the format for UTC date?

Times are expressed in UTC (Coordinated Universal Time), with a special UTC designator ("Z"). Times are expressed in local time, together with a time zone offset in hours and minutes. A time zone offset of "+hh:mm" indicates that the date/time uses a local time zone which is "hh" hours and "mm" minutes ahead of UTC.

What is the Z on DateTime?

The literal “Z” is actually part of the ISO 8601 DateTime standard for UTC times. When “Z” (Zulu) is tacked on the end of a time, it indicates that that time is UTC, so really the literal Z is part of the time.

What is UTC time now in 24 hour format?

UTC time in ISO-8601 is 08:40:06Z.


1 Answers

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

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

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

like image 51
hek2mgl Avatar answered Sep 19 '22 18:09

hek2mgl