12

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.

2
  • 1
    date('c', strtotime($date)); ? Otherwise, what have you tried. Commented Feb 13, 2013 at 8:38
  • @AmazingDreams I don't really know what to try... I want to get unix timestamp from string representation. Commented Feb 13, 2013 at 8:39

1 Answer 1

33

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I don't really care about milliseconds, this seems to be working. Thanks
Using 'DateTime::ISO8601' or 'c' instead of 'Y-m-d\TH:i:s+' in createFromFormat() also works...
I had to add the Timezone to make it work: DateTime::createFromFormat('Y-m-d\TH:i:s+', $isotime, new DateTimeZone('Etc/Zulu'));
@grim 'c' does not work.
Appendix to my prior comment. Rather use Y-m-d\TH:i:s.u\Z it works both ways.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.