9

I'm working on a web application where there will be many different users from all over the world making updates. I'm wondering what the best way to handle timezones would be? Ideally, as an event happens, all users view it using their local times.

Should the server OS time be set to GMT, physical location time, or my development location time?

Should the application logic (PHP) and database (MySQL) be set to store data as GMT or local time (local to users)?

Is there an industry standard or even a simple/obvious solution that I'm just not seeing?

4

Save your events in MySQL's TIMESTAMP format - it is stored internally as UTC regardless of the server / user timezone. This way the data is portable regardless of your server's specific configuration. If you store it in any specific timezone, you will have much more work converting it into different timezones.

Fetch it from the database into a numeric timestamp (using the UNIX_TIMESTAMP() function) for use with PHP's various date/time functions (such as date() ).

You then need to set for each user the PHP timezone using date_default_timezone_set() - you can get that information either via user configurable settings or from the browser headers (less accurate). You can then use PHP date/time functions as you would normally, and the output will be in the user's timezone.

Another alternative is to show relative time (for example: "5 hours and 2 minutes ago").

  • 5
    Be aware, that the (MySQL-) timestamp has a limited range from 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC, which for example doesn't allow to store birth dates (at least not for anyone older than 40 years). In fact the timestamp is the least suitable solution for storing date- and time-records. Use a datetime field and store everything as UTC. – feeela Mar 28 '12 at 12:12
7

I would use the UTC time in the database, and have the clients convert that to their local time when needed

  • UTC = GMT. I'm used to SQL Server – Rachel Feb 24 '11 at 17:38
  • 9
    UTC != GMT. UTC compensates for the increasing year duration caused by the Earth's slowing rotation while GMT does not. Though in practice the terms are interchanged, it's good to know the difference. en.wikipedia.org/wiki/Utc – user8685 Feb 24 '11 at 18:30
  • 1
    @DeveloperArt: Didn't realize that, thanks :) – Rachel Feb 24 '11 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.