Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

Comments for: ehresman20030911

Message # 1024396:
Date: 02/27/05 05:36
By: Ryan
Subject: I hate timezones....

That said, I've been trying to figure out how to handle them well in my own website and finally settled on this algorithm:

1. The first time someone visits a page on my site, PHP code will check for a cookie named cTimeOffset. This value, if it exists, is the number of minutes between the time on the user's computer and the time on the server.

2. If it doesn't find the cookie, it'll set the cookie to 0 and send a small snippet of JavaScript with the page able to calculate the time difference between the client and server.

3. The JavaScript is built with PHP to include the server's time. The client browser will calculate the time difference between the server and the client and store this number as a cookie in cTimeOffset. Additionally, it'll so a couple of sanity checks to make sure the number is within reason. Accounting for daylight savings and small errors in the time on the server or client machines, the time difference should never be more than about 1,500 minutes. If its larger than that, it'll set the offset to 0 assuming the time on the client's machine is so screwed up its not realiable.

3. Now for the life the cookie (which I set to the browser session, though you can use any amount of time that works best for you), subsequent page hits will come with the cTimeOffset cookie telling the server--to the minute--the time difference between it and the client. Additionally, these pages need no additional JavaScript--the time difference is already stored as a cookie.

The advantages for this system include:

1. Most people have their computers set to the correct time and since the time difference is calculated with every new session, unless the user happens to work past when daylight savings occur, the time will be accurate.

2. I don't need to write code to ask the user to enter their timezone, worry about daylight savings, store the information in a database, or update the code when an area of the world decides to change the rules governing their timezones or daylight savings.

3. I can store dates and times as the local server time, which I prefer since I have a hard time "thinking in UTC". If the server ever moves, it's not difficult to run a query that would add or subtract a couple of hours to all the timestamps in the database to keep it all running smoothly.

There are a couple of downsides to this approach, which for my purposes I've decided are acceptable:

1. It won't work if the client has disabled JavaScript or cookies on their browser. In that case, it'll default to the server's timezone. (Which, being on Pacific Time, would actually display the correct time to anyone living in that timezone anyhow--i.e. nearly half of my users.)

2. The first page a user visits, if it displays the time, will show the server's time. My site has to send the client the JavaScript first to set the correct time offset, and the server doesn't get that information back until the second page hit of a session.

For my site, all of the pages that would display the time require the user to be logged in, and therefore they'd have to go through a login page. Consequently, they'll never get to a page with the time on it on the first page hit.

And, you could probably send the JavaScript to set the cookie and immediately do a redirect or refresh the page to send the cookie to the server if it's THAT important that the first page a user sees has the correct times.

All-in-all, it's not a perfect system, but it works pretty darned well! Once it's working, it's very low maintenance and self-correcting--and I don't have to bother the user by asking them what timezone they're in.

Something to think about. =)

Happy trails!

-- Ryan

Previous Message | Next Message


Comments:
RE: Is ther something wrong with this method?Jason12/01/05 19:23
cofiguring iissujamary09/17/05 13:22
I hate timezones....Ryan02/27/05 05:36
Suneel Kanuri method beats all!Marius Ooms07/30/04 13:24
RE: A better way to go..Olav Alexander Mjelde07/07/04 05:30
A better way to go..Olav Alexander Mjelde04/09/04 15:30
RE: Well DoneOlav Alexander Mjelde03/18/04 17:58
Correction - I think GMT time storage is ok!Peter Matuchniak02/10/04 00:13
RE: Here is something i did with my timezones Vinutha02/07/04 04:33
Good ArticleRoger Rain01/11/04 06:56
Here is something i did with my timezones Suneel Kanuri11/25/03 10:53
TimeZone definition in EnvironmentNikola Savic10/07/03 05:53
It's much easier if you're on *nix...Jonathan09/22/03 16:42
Sometimes it's easier to store both...Peter Matuchniak, B.Sc (Maths)09/12/03 17:32
Well DonePaul09/12/03 10:51
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.