The subtle difference between
DateTime::ATOM (defined as
DateTime::ISO8601 (defined as
'Y-m-d\TH:i:sO') can be seen in the outputs of both formats in the following example:
$datetime = new DateTime('2021-01-03 02:30:00', new DateTimeZone('Europe/Berlin')); echo $datetime->format(DateTime::ATOM); // output: '2021-01-03T02:30:00+01:00' echo $datetime->format(DateTime::ISO8601); // output: '2021-01-03T02:30:00+0100'
The PHP documentation suggests to use
DateTime::ATOM instead of
DateTime::ISO8601 as the former is truly compatibility with ISO-8601 while the latter is not (it only exists for backward compatibility reasons).
The problem with the
DateTime::ISO8601 format is in the fact that the time zone designator part of the date/time string does not have a colon separating the hours and minutes. To understand why this is an issue, you must understand that ISO-8601 allows two formatting options for date and time and expects the entire string (i.e. the date, time, and time zone parts) to follow the same format. These two formatting options are as follows:
- Basic Format: No dashes separating the date parts and no colons separating the time parts (e.g.
- Extended Format: Requires separators such as dashes for date parts and colon for time parts (e.g.
DateTime::ISO8601 was following the "basic format", the correct implementation would have been
'Ymd\THisO' instead of
Hope you found this post useful. It was published . Please show your love and support by sharing this post.