Php – the best way to handle different TimeZones

MySQLPHP

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?

Best Answer

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").

Related Topic