0

I'm trying to parse following string:

 Thu Oct 03 2013 07:03:41 GMT+0200 (Central Europe Standard Time)

But I'm struggling to find the corresponding format, I tried:

  $date= DateTime::createFromFormat('D M d Y H:i:s eO (*)','Thu Oct 03 2013 07:03:41 GMT+0200 (Central Europe Standard Time)');
  echo $date->format('Y-m-d');

Which results in error. Problem is, that there is no space between GMT+0200 and the brackets. Following works just fine

$date= DateTime::createFromFormat('D M d Y H:i:s e O','Thu Oct 03 2013 07:03:41 GMT +0200');
echo $date->format('Y-m-d');

But (obviously) I should be able to parse also the first example. So do you have any suggestion how the correct format should look like?

the error I get:

Fatal error: Call to a member function format() on a non-object in C:\....

var_dump of $date before calling $date->format:

 boolean false
6
  • What error did it give Commented Oct 4, 2013 at 15:18
  • what is the error you see? also, are you missing an apostrophe in --> e O',[here?]Thu Oct <--- Commented Oct 4, 2013 at 15:20
  • yup sorry, ctrl+c ctrl+v typo. I also edited the question with error Commented Oct 4, 2013 at 15:23
  • what does var_dump($date) output before calling format() on it? Commented Oct 4, 2013 at 15:27
  • false (also added to question) Commented Oct 4, 2013 at 15:29

2 Answers 2

1

The problem is the format string, which should be

D M d Y H:i:s e+

I have replaced eO with just e because the input contains GMT+0200, which does not have a separator between "GMT" and the offset. I have also replaced the (*) part with +, which is the only specifier that can consume a variable amount of input (* matches one token, i.e. one word -- if there is more input afterwards the parse fails).

Note that there will still be a warning due to the use of + (use DateTime::getLastErrors to see it), but the conversion will work correctly.

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

2 Comments

Could you link to the docs for the use of '+'. I'm probably missing something, but can't find anything atm.
@vascowhite: php.net/manual/en/datetime.createfromformat.php, bottom of the table.
1

You could split the incoming string. Here is one way of skinning this particular cat:-

$dateString = 'Thu Oct 03 2013 07:03:41 GMT+0200(Central Europe Standard Time)';
\DateTime::createFromFormat('D M d Y H:i:s O', explode('(', $dateString)[0]);

See http://php.net/date for more help.

See it working.

2 Comments

Yes, I was thinking about the same thing, but trying to avoid that solution :-)
@user1762087 I can appreciate that. However, this method does avoid generating the warning that Jon's method produces. However, if you are not checking DateTime::getLastErrors() for the warning then that won't be an issue for you.

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.