How To Convert Dates Between Time Zones in PHP

Every developer has to cope with it sooner or later of their careerit’s far continually there. it is persistent, relentlessly ticking away and eating productivity. It continues music of when things appear and may cause you ache while looking to work with it. what’s it? Time (and time zones particularly).

We’ve all been there. we’ve our killer app, launched to the general public, then people begin reporting atypical dates. “What do you suggest? This works flawlessly,” you say to yourselfuntil you recognize that you, and everybody for your QA group that tested the application, are inside the identical time sectorwhilst a person from over again region uses your app – while that fateful day arrives, you first-class be compensating for it. otherwise, your statistics will get sketchy pretty speedy.

The Default Time Zone

Luckily, you continually have a baseline to paintings from the server’s time quartera few would argue that you should use UTC, but as long as something is the same old that everything is converted to, you have a recognized value to paintings from.

Every time you create a new date without giving it a time zoneit will be the server’s time quarter. From there we are able to convert it to something we need for the user. Likewise, if we keep the dates in a database based on the server’s time regionit is going to be clean to correlate activities because the entirety has been normalized.

Working with Dates as Objects

In PHP, one can work with dates as either an Object or old procedural code. In the first example, we are going to cover Objects.

 

Thanks to object-oriented design (OOP), converting a Date object is straightforward. Consider the following code:

 

echo 'America/Los_Angeles:<br>';
$date = new DateTime('2015-01-01', new DateTimeZone('America/Los_Angeles'));
echo $date->format('Y-m-d H:i:sP') . "<br><br>";

 

This will create a DateTime object in the Pacific time zone (America/Los_Angeles). Now, let’s say that we want to convert this date to the Eastern time zone.

 

echo 'America/New_York:<br>';
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:sP') . "<br><br>";

 

The second example takes the DateTime object we created in the first, and simply alters the time zone by calling setTimezone, and passing in the new time zone we want to use (America/New_York). Easy right?

Procedural

The procedural example works much the same, but uses function aliases to access all the same methods as the Object Oriented example:

 

echo 'Object Oriented<br>';
$date = date_create('2015-01-01', timezone_open('America/Los_Angeles'));
echo date_format($date, 'Y-m-d H:i:sP') . '<br><br>';
echo 'America/New_York:<br>';
date_timezone_set($date, timezone_open('America/New_York'));
echo date_format($date, 'Y-m-d H:i:sP') . '<br><br>'; 

 

As you can see, things are a bit more hodge-podge and not as easy as calling a function on an object, but it still accomplishes the same goal.

Gotchas

The important thing to doing date conversion is understanding your source. I repeat: recognize YOUR source. all the DateTime features within the global might not help when you have no concept what time area the source statistics is in. whilst working with internet packagesin case you are accepting any dates from the patron facetthey will most probably be within the consumer‘s time regionthat is something that wishes to be decided earlier than you may normalize the informationa few programs take care of this with the aid of having person settings that explicitly say what the time region is. Others depend totally on the againquit for dates.

Please follow and like us:



Leave a Comment

Your email address will not be published. Required fields are marked *