Welcome Guest, Not a member yet? Register   Sign In
Date Helper - Site with user specified timezones
#1

[eluser]section31[/eluser]
When developing a site where people can set their local timezones...What is the most efficient way to manage something like this?

I'm perplexed at how I would ever manage something that will completely be independent of the server's time.

I was thinking about using the Date Helper and store all timestamps as GMT, but how would I go about creating the gmt timestamp for their specified tmezone and converting it back. I don't think the date helper was ever intended to work with times like this, am I right?

Can someone enlighten me and explain to me how I should go about accomplishing this.

NOTE: I've posted a similar post on several other php forums in the past and nobody seems to be able to answer me. Surely there are some people out there that create websites that allow users to specify their own timezone.
#2

[eluser]Michael Wales[/eluser]
It's really easy actually - use timezone_menu(), or your own method that uses the same VALUES, to allow the user to store their timezeone.

On all actions that create a timestamp, use the now() method - which will generate a Unix timestamp based on your settings within config.php. Setup config.php to use gmt rather than local. I also like to store the timestamp as a MySQL DATE Field:
Code:
$insert['created_on'] = date('Y-m-d', now());

Then, when you are echo'ing out these date fields to the user (let's say, the time a post was made) you simply reference the created_on field with the user's timezone setting, via the gmt_to_local() method.

Code:
echo gmt_to_local(mysql_to_unix($post->created_on), $user->timezone, TRUE); // If DST is in effect
#3

[eluser]section31[/eluser]
oh my, i believe i was overthinking it. This makes perfect sense, thanks for the reply. I'll try something similar to this and see how it goes.
#4

[eluser]section31[/eluser]
One more question, how do those sites automatically know whether you're on daylight savings or not. There are some sites that don't even ask you.
#5

[eluser]section31[/eluser]
Anyone?
#6

[eluser]Dan Murfitt[/eluser]
I've hit the same wall with this. Looking at the code for gmt_to_local() it seems that the DST field simply adds an hour to the time:
Code:
if ($dst == TRUE)
{
    $time += 3600;
}
So I guess what you would need to do is store the user's timezone along with whether or not they are currently affected by DST. If you did this the user would need to set DST on and off twice a year as it happens to them.

I think the sites that do this automatically probably ask your location along with your time zone so they can adjust the DST value at the correct time (if needed). This is because (I think) DST occurs at different times for different locations. For example, Windows does this twice a year automatically, but you'll notice that a lot of time zones have more than one time zone with different locations. If you change the location, sometimes the 'automatically change my timezone' checkbox disappears (like the two GMT 0 ones). So, from your location, Windows will know if and when to set your DST value.

I hope this helps Smile
#7

[eluser]AgentPhoenix[/eluser]
Easiest thing to do is probably to just let the user decide if they DST or not. Most countries don't do DST (hell, not even all the states in the US do it), so letting the user decide is probably easiest.
#8

[eluser]Dan Murfitt[/eluser]
[quote author="AgentPhoenix" date="1216153125"]Easiest thing to do is probably to just let the user decide if they DST or not. Most countries don't do DST (hell, not even all the states in the US do it), so letting the user decide is probably easiest.[/quote]

Agreed Smile (looks like this forum does it too)
#9

[eluser]chuckleberry13[/eluser]
I've been looking into this as well and if your mysql server has the [email=http://dev.mysql.com/doc/refman/5.0/en/time-zone-support.html]mysql timezone files[/email] loaded then it can handle all the daylight savings adjustments automatically. Then all you have to do is set the timezone to the user's timezone per connection like this before you run your query.

So a query would look like this

Code:
SET time_zone = 'US/Eastern';
SELECT *  WHERE `timestamp` > CURDATE()

Timezone variables can also be values like this 'UTC' or '+10:00' or '-6:00'.

To test wether or not your mysql server has the timezone tables loaded run this query
Code:
SELECT COUNT( * )
FROM mysql.time_zone_name

My problem is my host currently doesn't have these tables loaded so I'm having to look into a php solution which the daylight savings issues seems like a guessing game. So I figured if you guys had these tables loaded it could save you a lot of trouble and get you accurate daylight savings times.

This is from the mysql documentation page "Per-connection time zones. Each client that connects has its own time zone setting, given by the session time_zone variable. Initially, the session variable takes its value from the global time_zone variable, but the client can change its own time zone with this statement:

mysql> SET time_zone = timezone;
The current session time zone setting affects display and storage of time values that are zone-sensitive. This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns. Values for TIMESTAMP columns are converted from the current time zone to UTC for storage, and from UTC to the current time zone for retrieval."
#10

[eluser]chuckleberry13[/eluser]
@ Michael your approach looks simple but you still run into the daylight savings problem. Over 80 something countries and most the US use daylight savings but have different starting and ending times. How do you account for this?




Theme © iAndrew 2016 - Forum software by © MyBB