Nickolaus Nickolaus - 8 months ago 40
PHP Question

Correct format pattern for string to DateTime

I have a string looking like this

, can anyone hint me what would be the correct format to in order to create a
object using
I have already tried some options but none of them seem to work until now.

Thanks in advance for the help.

Answer Source

DateTime has predefined constants for certain formats. Your date follows the ISO 8601 standard and its equivalent DateTime constant is called ATOM. So try:

$date = DateTime::createFromFormat(DateTime::ATOM, '2016-11-29T14:04:07+00:00');

Note that there is another constant called ISO8601 which is not compatible as stated in the documentation:

This format is not compatible with ISO-8601, but is left this way for backward compatibility reasons. Use DateTime::ATOM or DATE_ATOM for compatibility with ISO-8601 instead.