1

I have datetime strings like these:

2015-02-13 22:00 GMT+1 -DST

2015-03-20 21:00 GMT-5 +DST

What I started to try:

<?php
    $date = DateTime::createFromFormat('Y-m-d H:i ', '2015-03-20 21:00 GMT-5 +DST');
    echo $date->format('d-m-Y H:i');
?>

However I get stuck on the timezone and DST. I see on http://php.net/manual/en/datetime.createfromformat.php that I can use e, O, P and T for timezone, but I don't understand which character is representing what. Could I even use it directly like this? Do I have to convert that -5 to e.g. -0500? Then what character? And what to do with DST?

2 Answers 2

4

That date string isn't common and has some unnecessary information in it (at the end). To parse it I just tell DateTime::createFromFormat() to ignore the GMT and +DST parts by using the + character. The O character is smart enough to handle -5 and convert it to -0500. If we don't ignore them the timezone is incorrect in the new DateTime object.

<?php
    $date = DateTime::createFromFormat('Y-m-d H:i +O +', '2015-03-20 21:00 GMT-5 +DST');
    echo $date->format('d-m-Y H:i');

Demo

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

3 Comments

Thanks, but that doesn't seem to be getting the timezone right: eval.in/291852 - It does work like this: eval.in/291854 but then I have to remove the dst.
Yep :) I wanted to be sure that timezone was correct in case it mattered to your project.
Thanks :) now I can just use the full original string.
1

I found this at http://php.net/manual/en/function.date.php. And yes I believe you can use it directly.

e - Timezone identifier (added in PHP 5.1.0) Examples: UTC, GMT, Atlantic/Azores

O - Difference to Greenwich time (GMT) in hours Example: +0200

P - Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) Example: +02:00

T - Timezone abbreviation Examples: EST, MDT ...

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.