I was reading documentation of an API and it uses this timestamp format:
2012-08-29T15:31:23Z
I have never seen one with T and Z positioned like that so I had to create a custom function to product this format.
Is there a built-in PHP method of producing a timestamp in this format or a standard?
This is universal time .... it is the date then a time separator and time is put into zulu time which is the same as UTC
You are able to do a simple print strtotime('2012-08-29T15:31:23Z'); to change it to a unix timestamp
FROM W3
Example
1994-11-05T08:15:30-05:00 corresponds to November 5, 1994, 8:15:30 am, US Eastern Standard Time.
1994-11-05T13:15:30Z corresponds to the same instant.
ISO 8601 date time format specifications
http://www.w3.org/TR/NOTE-datetime
"...
Examples
1994-11-05T08:15:30-05:00 corresponds to November 5, 1994, 8:15:30 am, US Eastern Standard Time.
1994-11-05T13:15:30Z corresponds to the same instant.
..."
It's in the format standardized by ISO 8601. The Z is for the UTC timezone.
You can parse it with strtotime and create your own with date (the DATE_ constant comes a list of DateTime Constants, which is linked from the date manpage):
$timestamp = strtotime('2012-08-29T15:31:23Z');
$datetime = date(DATE_ISO8601, $timestamp);
Related
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
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());
Why
echo date('c');
not equal with
$datetime = new DateTime();
echo $datetime->format(DateTime::ISO8601);
The result:
2016-07-07T21:18:22+03:00
2016-07-07T21:18:22+0300
Both must give current time in ISO8601 format. In Wikipedia right format is 2016-07-07T21:18:22+03:00 but some banks use 2016-07-07T21:18:22+0300 format in API. Why?
2016-07-07T21:18:22+03:00
Is the correct ISO 8601:2004 representation.
2016-07-07T21:18:22+0300
Is incorrect, the zone designator may not be in the basic format when the date and time of day is in the extended format.
ISO 8601:2004 4.3 Date and time of day:
[...] the expression shall either be completely in basic format, in
which case the minimum number of separators necessary for the required
expression is used, or completely in extended format, in which case
additional separators shall be used [...]
Update 1:
ISO 8601 specifies three differerent date representations: calendar, ordinal and week dates. Theese can be formatted in either basic format (minimum number of seperators) or extended format (extension of the basic format that includes additional separators). ISO 8601 requires that the resulting expression is either consistently in basic format or consistently in extended format.
Combination of calendar date and time of day in local time with difference from UTC:
2016-07-07T21:18:22+03:00 (extended format)
20160707T211822+0300 (basic format)
Combination of ordinal date and time of day in local time with difference from UTC:
2016-189T21:18:22+03:00 (extended format)
2016189T211822+0300 (basic format)
Combination of week date and time of day in local time with difference from UTC:
2016-W27-4T21:18:22+03:00 (extended format)
2016W274T211822+0300 (basic format)
All of the above representations represent the same date and time of day in local time with difference from UTC (and instant). If an API documents that it accepts an ISO 8601 date with time of day and a zone designator (aka known as a complete representation), it should accept all of the above representations to be compliant with ISO 8601.
Update 2:
Most bugs i have encountered stem from using strftime() to output an ISO 8601 date and time of day in local time with difference from UTC in extended format. The standard strftime() can only output compliant representations in basic format due to the limitations in the z conversion specifier:
Combination of calendar date and time of day in local time with difference from UTC:
Format: Example:
%Y%m%dT%H%M%S%z 20160707T211822+0300
Combination of ordinal date and time of day in local time with difference from UTC:
Basic format: Example:
%Y%jT%H%M%S%z 2016189T211822+0300
Combination of week date and time of day in local time with difference from UTC:
Basic format: Example:
%GW%V%uT%H%M%S%z 2016W274T211822+0300
The GNU strftime implementation supports a : flag between the percent and the z conversion specifier to specify that the zone designator should be formated in extended format:
Combination of calendar date and time of day in local time with difference from UTC:
Format: Example:
%Y-%m-%dT%H:%M:%S%:z 2016-07-07T21:18:22+03:00
Combination of ordinal date and time of day in local time with difference from UTC:
Format: Example:
%Y-%jT%H:%M:%S%:z 2016-189T21:18:22+03:00
Combination of week date and time of day in local time with difference from UTC:
Format: Example:
%G-W%V-%uT%H:%M:%S%:z 2016-W27-4T21:18:22+03:00
Still PHP does not interpret ISO8601 correctly when milliseconds are given, even with DateTime::ATOM.
$d=DateTime::createFromFormat(DateTime::ATOM,"2018-01-10T01:00:00.000Z");
// null
or using Carbon:
echo Carbon\Carbon::createFromFormat(Carbon\Carbon::ATOM,"2018-01-10T01:00:00.000Z","UTC");
// InvalidArgumentException with message 'The timezone could not be found in the database'
The best way is to let Carbon or datetime figure it out itself:
$d = new Carbon\Carbon("2018-01-10T01:00:00.000Z");
// -> 2018-01-10 01:00:00
$d = new Carbon\Carbon("2018-01-10T01:00:00Z");
// -> 2018-01-10 01:00:00
I have two date:
2014-12-01T12:05:59Z //dont know which format
And
2014-12-01T03:59:00-08:00 //i'll get it from date("c") function in php (ISO 8601).
So, I don't know what is the different in this date's?
And how to convert date in 2014-12-01T12:05:59Z this formate in php?
[UPDATE]
I want to get current timestamps in in 2014-12-01T12:05:59Z this date formate.
It's the ISO 8601 format and both time zone variants are allowed and must be expected. -08:00 means 8 hours behind UTC, Z means UTC and is a shortcut for +00:00.
Both the dates you have mentioned are dates with timezone. The only difference is that the first date has 'z' at the end which is a Zone Designator for UTC and the later date is suffixed with timezone offset, which is generally represented as "HH:MM". Practically, both dates are same and both the representations are correct.
The 'z' suffixed representation is generally accepted xml standard for timestamp in API payloads.
Looking at your requirements, since your API provider needs the date with Zone Designator, they must be calculating time in UTC.
I will suggest changing your timezone to UTC first using
date_default_timezone_set('UTC');
and then use this expression to get required timestamp
date('Y-m-d\TH:i:s\Z', time());
This way you are saved from the timezone conflict and you will also be able to send the required date to the API.
date_default_timezone_set('UTC');
echo gmdate('Y-m-d\TH:i:s\Z');
Check the link. You will get the difference as well.
I am using strtotime() to get a timestamp from a date and time string. I will be running strtotime() during the summer (daylight savings) to give me a timestamp of a winter date (non-daylight savings).
In the winter, I will need to convert my timestamp to a readable date using date() -- will it be the same date/time I put into strtotime() during the summer?
On each one of my pages, I am setting my timezone by date_default_timezone_set with my city.
So, running this during the summer (daylight savings):
date_default_timezone_set('America/Los_Angeles');
echo strtotime("Dec 1 2014 8:00 am");
Gives me a certain timestamp 1417449600.
Will running this during the winter (non-daylight savings) return 8:00am as I need it to do?
date_default_timezone_set('America/Los_Angeles');
echo date("g:ia",1417449600);
Yes. If the timezone you set is doesn't explicitly say whether it's standard or daylight-savings time, it automatically determines the state of DST from the time that you give it and the rules for when the timezone switches into and out of DST.
Yes. A UNIX timestamp such as 1417449600 represents a completely, globally, universally unique point in time, independent of fussy timezone notation. There's only one "December 1st 2014 8 am in Los Angeles", which is the same point in time as "December 1st 2014 17:00 CET" and a number of other local notations across the world. The UNIX timestamp 1417449600 expresses that point in time, regardless of whatever your wall clock says exactly.
When you format this unique point in time back to a more human readable format using date(), it figures out what exactly the time must be formatted at based on the set timezone. It won't change based on what the time or DST settings are now.
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
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());