What's the Difference Between PHP DateTime::ATOM and DateTime::ISO8601?

DateTime::ATOM vs. DateTime::ISO8601

The subtle difference between DateTime::ATOM (defined as 'Y-m-d\TH:i:sP') and 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:

  1. Basic Format: No dashes separating the date parts and no colons separating the time parts (e.g. 20210103T023000+0100);
  2. Extended Format: Requires separators such as dashes for date parts and colon for time parts (e.g. 2021-01-03T02:30:00+01:00).

If DateTime::ISO8601 was following the "basic format", the correct implementation would have been 'Ymd\THisO' instead of 'Y-m-d\TH:i:sO'.

Hope you found this post useful. It was published . Please show your love and support by sharing this post.