Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Convert ISO8601 Offset To Actual Time Zone

Tags:

php

datetime

I'm connecting to an JSON event API that returns schedule information in ISO8601 format.

"schedule": [
{
    "datetime": "2014-06-02T19:30:00-04:00",
    "speaker": "Keith Moore"
},
{
    "datetime": "2014-06-03T19:30:00-04:00",
    "speaker": "Phyllis Moore"
]

When I echo date("c", strtotime($schedule[$j]["datetime"])); I get 2014-06-02T18:30:00-05:00. Of course, that makes sense since date() uses the server's time zone.

If I pass a time zone to date(), or use date_default_timezone_set), then it should fix things up. Unfortunately, there is no specification as to what the time zone is in ISO8610 (just the offset). If I try and get the time zone from the ISO8601 date, using date(), then the time zone goes back to the server's.

What I want to do is show the date/time/time zone that's local to the event (Eastern Daylight Time, in this instance). Is there a way to do this?

A couple options that I have thought of, but neither seems appropriate...

Since I have control over the API, I could send the schedule information in RFC 822 format (Mon, 02 Jun 2014 19:30:00 EDT). That would give me hooks that I could do...

if ($pubDatetimezone == "PST" || $pubDatetimezone == "PDT") date_default_timezone_set("America/Los_Angeles");
if ($pubDatetimezone == "MST" || $pubDatetimezone == "MDT") date_default_timezone_set("America/Denver");
if ($pubDatetimezone == "CST" || $pubDatetimezone == "CDT") date_default_timezone_set("America/Chicago");
if ($pubDatetimezone == "EST" || $pubDatetimezone == "EDT") date_default_timezone_set("America/Montreal");

...but ISO8601 just seems like a more appropriate format.

On the other hand, I could do some overly-complex array where I specify every possible date/offset combination with a time zone. That, definitely, seems like too much work.

like image 935
doubleJ Avatar asked Jan 22 '26 16:01

doubleJ


1 Answers

You should use PHP's DateTime. Since ISO8601 is an international standard, PHP supports it. The code you need is the following:

$dt = DateTime::createFromFormat(DateTime::ISO8601, '2014-06-03T19:30:00-04:00');

You can specify the third parameter, a DateTimeZone object which you can use to further offset the given date.

For example, if you wanted to use GMT -4 to be GMT

$dt = DateTime::createFromFormat(DateTime::ISO8601, '2014-06-03T19:30:00-04:00', new DateTimeZone('Europe/London'));

And after that, all that's left is:

echo $dt->format('d.m.Y, H:i:s'); // format is the same as for date() function.
like image 91
N.B. Avatar answered Jan 24 '26 09:01

N.B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!