Home » Php » date – How to get timestamp in ISO 8601 format with timezone in php

date – How to get timestamp in ISO 8601 format with timezone in php

Posted by: admin July 12, 2020 Leave a comment

Questions:

Based on the wix documentation, 2014-04-16T11:16:27.930Z is a timestamp in ISO 8601 format with a timezone.
A quick research reveals that the timestamp in ISO 8601 displays the timezone with +time_interval (for instance +00:00)

I tried date(‘c’) which displays: 2014-04-16T06:23:31+00:00

Could anyone tell me how to display timestamp in 2014-04-16T11:16:27.930Z rather than 2014-04-16T06:23:31+00:00

How to&Answers:

Considering the Wikipedia Article on ISO_8601 the UTC Offset can be defined as a Hour:Minutes Definition of as a HoursMinutes Definition.

Z is the zone designator for the zero UTC offset. "09:30 UTC" is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would be "14:45:15Z" or "144515Z".

The PHP date method defines the parameter Z as

Z Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.

So assuming the offset mentioned in the wikipedia article is in seconds, you could create your own ISO 8601 using date. Example given for current server time/date:

date('Y-m-d\TH:i:s.Z\Z', time());

Also, as mentioned in the comments by @AndrewIsOffline, since PHP5, using ‘c’ will also give you the ISO 8601 Date:

date('c', time());