0

I have been given a date string from an API in the format e.g.

20120522T143127

I am trying to convert this to a DateTime object, but the object creation fails because of the 'T' (I think).

This is my current code:

$date   = DateTime::createFromFormat( "YMDTHis", '20120522T143127' );
$result = $date->format( $format );

What am I missing?

I also tried:

$date   = DateTime::createFromFormat( "YMD\THis", '20120522T143127' );
4
  • Can you tell why this T is there? Commented Jul 20, 2015 at 10:20
  • Returned datetime formatting from calling an API - out of my hands. Commented Jul 20, 2015 at 10:24
  • check @Rizer answer. Commented Jul 20, 2015 at 10:25
  • You have YMD when you need to have Ymd. And btw: this is standard format, so just use new DateTime('20120522T143127') Commented Jul 20, 2015 at 10:57

2 Answers 2

3

Your format is a bit wrong:

  • M A short textual representation of a month, three letters Jan through Dec

    • Use m instead of M
    • m Numeric representation of a month, with leading zeros 01 through 12
  • D A textual representation of a day, three letters Mon through Sun

    • Use d instead of D
    • d Day of the month, 2 digits with leading zeros 01 to 31
  • Just escape the T with a backslash

So just use:

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

Comments

0
echo date('Y-m-d H:i:s',strtotime('20120522T143127'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.