Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Parse date in ISO format [duplicate]

Tags:

php

datetime

I'm currently using ConstantContact which returns XML with updated field in format like this:

2013-02-13T08:35:34.195Z

I'm assuming this is date('c') format. How to parse this format? strtotime isn't returning correct value.

like image 398
ewooycom Avatar asked Feb 13 '13 08:02

ewooycom


1 Answers

You may want to take a look at the DateTime::createFromFormat() function.

$datetime = DateTime::createFromFormat('Y-m-d\TH:i:s+', '2013-02-13T08:35:34.195Z');

The problem with that is you're going to loose the milliseconds.

The + sign in the format string simply tells this function to ignore the rest of the string instead of creating an error.

Confirmed in PHP7.2. As in comment below you can use Y-m-d\TH:i:s.u\Z to match the exact Js string that toISOString gives.

like image 73
MarcDefiant Avatar answered Sep 23 '22 14:09

MarcDefiant