I am trying to integrate a timezone system in my app, i’ve really tried hard on avoiding making timezone-aware apps upto now – but its a mandatory requirement now so got no choice. TimeZones it just goes over my head. I’ve read several topics on PHP.net and also other sites including but not limited to SO. But i never could get the hang of it.
So i was wondering if some one can help me out here 🙁 What i’m looking to make is a preference option in my app to allow users to choose their own timezones from a select menu but the app should also be able to SET/Choose the DST accordingly itself for each user.
Please i’m sure this will help others who are still striving to get the hang of the timezones, so please provide as much detailed explanation as possible, even if you have to consider me a complete dumbo/noob.
Edit for bounty:
I am adding a bounty to this question because I really thing we need a good canonical question about time zones when writing PHP/MySQL apps (thus I’m also adding the MySQL tag). I have found things from many places, but it would be good to have it all together. Charles’ answer is great, but I still feel it’s lacking somewhat. Here are some things I thought of:
- How to store the times in the database from a PHP
DateTimeobject - Should they be stored in
DATETIMEorTIMESTAMP? What are the benefits or caveats for each? - Do we ever need to worry about time zones with MySQL
DATE? - How to insert values using
NOW(). Do these need to be converted somehow either before or after the insert? - Is it necessary to set the time zone used by MySQL? If so, how? Should it be done persistently or upon every HTTP request? Does it have to be set to UTC or can it be anything else? Or is the server’s time sufficient?
- How to retrieve values from MySQL and convert them to a
DateTimeobject. Will putting it straight intoDateTime::__construct()suffice or do we need to useDateTime::createFromFormat()? - When to convert to local time and why. Is there ever a time that we would want to convert it before it is echoed back to the user (e.g. to compare to another DateTime object or a static value)?
- Is there ever a time we need to worry about Daylight Savings Time (DST)? Why or why not?
- What should someone do if they have previously inserted data (e.g. using
NOW()) without worrying about the time zone to make sure everything stays consistent? - Anything else you think of that someone should look out for
If possible, try to separate it into logical sections to make it easier for future users to find the information. Be sure to provide code examples where necessary.
This answer has been updated to accomodate the bounty. The original, unedited answer is below the line.
Almost all of the question points added by the bounty owner are in relation to how MySQL and PHP datetimes should interact, in the context of timezones.
MySQL still has pathetic timezone support, which means that the intelligence has to be PHP-side.
NOW(), to be handled sanely.DATETIME, never useTIMESTAMPunless you very expressly require the special behavior in aTIMESTAMP. This is less painful than it used to be.Y-m-d H:i:sThat addresses most of the points.
The last thing is a doozy:
This is a real annoyance. One of the other answers pointed out MySQL’s
CONVERT_TZ, though I’d personally have done it by hopping between server-native and UTC timezones during selects and updates, ’cause I’m hardcore like that.You don’t need to and should not do this in the modern era.
Modern versions of PHP have the DateTimeZone class, which includes the ability to list named timezones. Named timezones allow the user to select their actual location, and have the system automatically determine their DST rules based on that location.
You can combine DateTimeZone with DateTime for some simple but powerful functionality. You can simply store and use all of your timestamps in UTC by default, and convert them to the user’s timezone on display.
By using this technique, the system will automatically select the correct DST settings for the user, without asking the user whether or not they’re currently in DST.
You can use a similar method to render the select menu. You can continually reassign the time zone for the single DateTime object. For example, this code will list the zones and their current times, at this moment:
You can greatly simplify the selection process by using some client-side magic. Javascript has a spotty but functional Date class, with a standard method to get the UTC offset in minutes. You can use this to help narrow down the list of likely timezones, under the blind assumption that the user’s clock is right.
Let’s compare this method to doing it yourself. You’d need to actually perform date math every single time you manipulate a datetime, in addition to pushing a choice off on the user that they aren’t going to really care about. This isn’t just sub-optimal, it’s bat-guano insane. Forcing users to signify when they want DST support is asking for trouble and confusion.
Further, if you wanted to use the modern PHP DateTime and DateTimeZone framework for this, you’d need to use deprecated
Etc/GMT...timezone strings instead of named timezones. These zone names may be removed from future PHP versions, so it’d be unwise to do that. I say all of this from experience.tl;dr: Use the modern toolset, spare yourself the horrors of date math. Present the user with a list of named time zones. Store your dates in UTC, which won’t be impacted by DST in any way. Convert datetimes to the user’s selected named time zone on display, not earlier.
As requested, here’s a loop over the available time zones displaying their GMT offset in minutes. I selected minutes here to demonstrate an unfortunate fact: not all offsets are in whole hours! Some actually switch half an hour ahead during DST instead of a whole hour. The resulting offset in minutes should match that of Javascript’s
Date.getTimezoneOffset.