What is the right ISO8601 format? - php

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

Related

PHP convert ISO 8601 timestamp to unix then back to ISO 8601 [duplicate]

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

What is the difference between these two date?

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.

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

What is the time format used in facebook created date?

Hi i am working on facebook Graph API where i need all the posts information of a group. So I did it and saw [created_date'] => '2013-01-25T00:11:02+0000' what does this date and time represent i mean i know 2013-01-25 is date and 00:11:02 is time but what does T and +0000 represent.
BTW where is the server of facebook. Which timestamp should i use to match facebook time?
Thank you.
T = TIME and the +0000 is timezone offset. Facebook uses localized timezones. You can request a Unix timestamp instead of the string by adding the parameter: date_format=U to your graph API call.
Please see this link for more information.
The date format is called ISO 8601. The letter T is used to separate date and time unambiguously and +0000 is used to signify the timezone offset, in this case GMT or UTC.
That said, you generally don't need to worry so much about the actual contents; rather you should know how to work with them. To use such a date, you can use strtotime() to convert it into a time-stamp:
$ts = strtotime('2013-01-25T00:11:02+0000');
To convert the time-stamp back into a string representation, you can simply use gmdate() with the predefined date constant DATE_ISO8601:
echo gmdate(DATE_ISO8601, $ts);
Alternatively, using DateTime:
// import date
$d = DateTime::createFromFormat(DateTime::ISO8601, '2013-01-25T00:11:02+0000');
// export date
echo $dd->format(DateTime::ISO8601), PHP_EOL;
This is a standard format, specifically ISO 8601.
As much as I don't like linking to it, http://www.w3schools.com/schema/schema_dtypes_date.asp does have a good "human-understandable" explanation:
The dateTime is specified in the following form "YYYY-MM-DDThh:mm:ss"
where:
YYYY indicates the year
MM indicates the month
DD indicates the day
T indicates the start of the required time section
hh indicates the hour
mm indicates the minute
ss indicates the second
To specify a time zone, you can either enter a dateTime in UTC time by
adding a "Z" behind the time - like this:
2002-05-30T09:30:10Z
or you can specify an offset from the UTC time by adding a positive or
negative time behind the time - like this:
2002-05-30T09:30:10-06:00
or
2002-05-30T09:30:10+06:00
Therefore, in your case the +0000 indicates a time offset of 0 from UTC.

Can someone tell me what date format this is?

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

Categories