Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mortie/b10cba988daf37236b4db34b8d307951 to your computer and use it in GitHub Desktop.
Save mortie/b10cba988daf37236b4db34b8d307951 to your computer and use it in GitHub Desktop.
ISO8601 lolphp

No it's not valid ISO8601

ISO8601 has multiple valid representations

that is true, for example

Basic format: YYYYMMDD Example: 19850412
Extended format: YYYY-MM-DD Example: 1985-04-12 

making PHP's DateTime::ISO8601 completely within the specification of the standard

that's not true (or at the very least it's not true in ISO8601:2004, the only revision i have access to), the first part 1970-01-01T01:00:00 is a valid extended-format, and now for adding the timezone, in BASIC format, this is legal (per section 4.3.2 Complete representations):

YYYYMMDDThhmmss±hhmm 19850412T101530+0400
 YYYYMMDDThhmmss±hh 19850412T101530+04 
  • that's legal in basic format, and is how PHP adds the timezone, but that's not legal in extended format, here's how to add it in extended format:
Extended format: YYYY-MM-DDThh:mm:ss Example: 1985-04-12T10:15:30
 YYYY-MM-DDThh:mm:ssZ 1985-04-12T10:15:30Z
 YYYY-MM-DDThh:mm:ss±hh:mm 1985-04-12T10:15:30+04:00
 YYYY-MM-DDThh:mm:ss±hh 1985-04-12T10:15:30+04
  • now what does php do? it starts out with legal extended format illegal basic format 1970-01-01T01:00:00 ... and then it ends with legal basic format illegal extended format: +0100 - in all of ISO8601:2004 i can't find a single example where the standard is mixing extended format with basic format, and i can't find a single statement saying "you're allowed to mix basic format with extended format" either. PHP's DateTime::ISO8601 format is a mix of starting with legal extended format, and ending with legal basic format, that mix is not allowed per ISO8601:2004.

edit: found this, which quite clearly states that mixing extended with basic is NOT legal, quoting section 4.3.3 of ISO8601:2004:

For reduced accuracy, decimal or expanded representations of date and time of day, any of the
representations in 4.1.2 (calendar dates), 4.1.3 (ordinal dates) or 4.1.4 (week dates) followed immediately by
the time designator [T] may be combined with any of the representations in 4.2.2.2 through 4.2.2.4 (local time),
4.2.4 (UTC of day) or 4.2.5.2 (local time and the difference from UTC) provided that
(...skipped stuff...)
d) 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 in accordance with 4.1 and 4.2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment