I allow my users to add events to my webapp. Users can originate from all around the world.
My current idea is to determine the user’s time zone automatically (using an IP to location API) and insert it into the users table (I will also allow them to change it).
On the events table I will insert the event start date/end date in UTC.
Then, whenever I need to display the event info, I would take the event start date/end date from the table and do the calculation against the user’s timezone.
Is that considered as good practice or is there a better way to do that?
Anything I should be aware of when doing this?
Thanks,
Yes, that is indeed a good way. Also keep in mind that if you use the
TIMESTAMPtype in MySQL, MySQL handles the timezone converting for you. When you insert new dates/times to the database, MySQL converts it from the connection’s timezone to UTC (TIMESTAMPis always stored in UTC). When you retrieve aTIMESTAMPfield from database, MySQL converts it back to the connection’s timezone.So if you use
TIMESTAMPfields in MySQL, all you need to do is tell the user’s timezone to MySQL at start of each your page. You do so by:You can also use numeric timezones:
Keep in mind that you might need to install the tzinfo to MySQL first, which is trivial though (only for the non-numeric version though). Here’s information about how to do it: http://dev.mysql.com/doc/refman/5.0/en/mysql-tzinfo-to-sql.html
In a nutshell, this is the important part:
Here’s an example of how it works:
If you don’t use
TIMESTAMPfield, eg. you useDATETIME(which also supports wider range of dates), then you just need to make sure you always insert dates in UTC; I do this by always setting connection’s timezone to+00:00. Then you can have a view helper in PHP that converts the datetime to the user’s timezone, which is quite trivial to do with PHP’s DateTime class and setTimezone function. There’s an example in the last link. To use this method, you must make sure PHP is also set to use UTC as its default timezone, which you can do with this:Whichever method you use, you should always be aware of these facts:
TIMESTAMPandDATETIMEtypes with each otherTIMESTAMPtype, set the timezone to the user’s timezoneDATETIMEtype, set the timezone to UTC and handle timezone convertions in PHP