Reputation: 21323
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:
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
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.
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
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:
When a page is loaded, use Javascript to get the UTC timezone offset:
new Date().getTimezoneOffset();
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.
For all subsequent database openings, run the query SET time_zone="$Timezone"
, where $Timezone
is the value cached in the session.
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
Reputation: 254886
I'd propose another solution:
timestamp
column typeSET 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):
Date.getTimezoneOffset()
) and send it with ajaxUpvotes: 2
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