I’m developing a web application that involves the use of timestamps. I would like the timestamps to display in the user’s time zone with as little configuration as possible. Facebook and Google Calendar both update their displayed time automatically when logged into from different time zones.
What is the most effective method you’ve used to deal with time zones in your web apps? If at all possible, I would like to require no additional input from the user.
Here’s what I’m thinking so far:
- Store all timestamps in the database in UTC
- Keep a database table that correlates time zones to UTC offsets, and whether daylight savings time is observed in that time zone
- Given client information (IP address?), find the user’s current time zone. This information can be cached in a session variable to prevent repeated reads to the table.
- Whenever a timestamp from the database is to be displayed, convert it using the UTC offset
I find it hard to believe that there’s nothing out there already that does this. If you have any suggestions, I’d appreciate it.
I’d propose another solution:
timestampcolumn typeSET time_zone='<tz>'where<tz>can be either+01:00offset or timezone nameAsia/Vladivostok(for the latter special timezones db should be imported by DBA)The benefit of using
timestampis that it automatically converts the date to and from UTC, according to thetime_zoneyou’ve specified in the current connection.More details about timestamp: http://dev.mysql.com/doc/refman/5.1/en/timestamp.html
About retrieving current user’s timezone (even though there are a lot of answers here on SO about this topic, let’s do that once again):
Date.getTimezoneOffset()) and send it with ajax