Every developer has to cope with it sooner or later of their career. it’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 yourself. until you recognize that you, and everybody for your QA group that tested the application, are inside the identical time sector. whilst 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 quarter. a 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 zone, it 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 region, it 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?
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.
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 packages, in case you are accepting any dates from the patron facet, they will most probably be within the consumer‘s time region. that is something that wishes to be decided earlier than you may normalize the information. a 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 again–quit for dates.