Zach Rattner
Zach Rattner

Reputation: 21323

What is the most effective way to manage time zones in a PHP/MySQL application?

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:

  1. Store all timestamps in the database in UTC
  2. Keep a database table that correlates time zones to UTC offsets, and whether daylight savings time is observed in that time zone
  3. 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.
  4. 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.

Upvotes: 2

Views: 2978

Answers (4)

Eugine Joseph
Eugine Joseph

Reputation: 1558

If you like to solve this issue using PHP, check this out.
The following code can be used to find the local time and the Greenwich Mean Time(GMT).

<?php  
echo date("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));    # Local Time 
echo gmdate("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));  # GMT Time 
?>

Use these steps to show the time based on location.

  1. Add a Session variable which stores the difference of local and GMT time.
  2. Add the difference to the GMT time before showing in the site.

For example,

# Here shows the difference 
$diff = (strtotime(gmdate("M d Y H:i:s",time())) - strtotime(date("M d Y H:i:s",time())));  

echo '<br />'.(date("M d Y H:i:s",time()));          #  Oct 16 2013 08:09:23
echo '<br />'.gmdate("M d Y H:i:s",(time()-$diff));  #  Oct 16 2013 08:09:23

Upvotes: 0

Zach Rattner
Zach Rattner

Reputation: 21323

Ok, I've thought about it some more, used the info posted by some other people, and here's what I think the best approach is:

  1. When a page is loaded, use Javascript to get the UTC timezone offset:

    new Date().getTimezoneOffset();

  2. Send this value back via AJAX to a PHP script. The PHP script formats the data into the form +hh:mm or -hh:mm, and then stores this value in the current session.

  3. For all subsequent database openings, run the query SET time_zone="$Timezone", where $Timezone is the value cached in the session.

  4. Use the MySQL TIMESTAMP type for all columns containing time data.

I like this approach because it lets MySQL do all the dirty work, and requires no huge time zone database that I have to manage.

What are your thoughts?

Upvotes: 0

zerkms
zerkms

Reputation: 254886

I'd propose another solution:

  1. Store all the dates in mysql timestamp column type
  2. Right after connect to mysql - specify current user's timezone with SET time_zone='<tz>' where <tz> can be either +01:00 offset or timezone name Asia/Vladivostok (for the latter special timezones db should be imported by DBA)

The benefit of using timestamp is that it automatically converts the date to and from UTC, according to the time_zone you'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):

  1. As long as there is a TZ value in cookies (look at step 4) - use it.
  2. For registered/authenticated users - you can ask to specify their timezone and store it permanently in DB.
  3. For guests you can retrieve the timezone from javascript (Date.getTimezoneOffset()) and send it with ajax
  4. If it is the first request/nothing passed from ajax - guess it by IP
  5. For each step (1-3) - save timezone to a cookie.

Upvotes: 2

evan
evan

Reputation: 12535

I think what you have is a great solution.

The only alteration is in getting the user's timezone. All you need to do is have bit of javascript report the user's time. Then you know the offset. That works even if the IP address is the same but the user updates the computer's timezone because it was incorrect for some reason.

Upvotes: 0

Related Questions