lomse
lomse

Reputation: 4165

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

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

Upvotes: 13

Views: 26339

Answers (1)

Andresch Serj
Andresch Serj

Reputation: 37418

Considering the Wikipedia Article on ISO_8601 the UTC Offset can be defined as "Year-Month-Day Hour:Minutes:Seconds.ZoneDesignator".

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());

Upvotes: 15

Related Questions