views:

132

answers:

2

I have a social network site I have been working on for a couple years in PHP/MySQL, I am now re-buidling the whole site from scratch again though. This time around I would really like to add in the ability for users to set a timezone for times on my site.

So with that said, I am asking on a guide from start to finish of the steps I need to include to do this please. My old site used mysql datetime for all dates and times and it worked great but I read that it is best to use like a text filed and store all dates and times with UTC, can someone explain how I could do this? Would I still be able to use the now() function in php to save a time to the mysql?

Also I have seen the list that php can generate of all the timezones, the one where it shows like a million (not really) but I am wondering, would it be possible to show some sort of map with images or something and link to the main timezones?

Please any tips for setting a users timezone, I can do that part, but once I have a user's timezone saved and ready to use, how can I make sure the users see's the correct time and how do I save times in the correct time.

Sorry if this was confusing, any help would be great though, thanks.

+1  A: 

This topic gets complicated especially when you factor in daylight savings and the fact that your server will be in a different time zone from some of your members.

I suggest you take a look at some libraries out there that have been built to handle these issues. The PEAR package Date might be a good place to start. http://pear.php.net/package/Date

Basically, you would want your members to select a timezone for their profile. Then convert all times to UTC (to remove the offset of the server location) and store them in your database as a timestamp. When displaying the times, apply the timezone offset the member chose for themself (maybe with UTC as the default) as well as a date display format.

kdevine
+2  A: 

When working with php, from my experience, it is the best to store timestamps, generated with time(), as an int in the database. While it may not be that easy to read them when looking at the database (as you cannot guess the actual date just from the number), but the timestamp is the most-native thing when working with dates in php.

To save each user's timezone, you can simply save the offset, in either hours, or better (as some timezones are half an hour off) in minutes. So for example my timezone offset would be +60 (UTC+1), while in America most will have something like -480 (UTC-8) or similar.

Then when displaying the times for a user, you just pick the timestamp (which is in UTC time) and the timezone offset and generate a readable date from it using the standard date() function for formatting.

For example:

<?php
$time   = time(); // from database or time() for "now"
$offset = -480;   // from database

// add the offset to the time you want to display and you have the user's time
echo date( 'd.m.Y H:i', $time + $offset * 60 );
?>

In addition you could then also store the formatting string (d.m.Y H:i) in the database, so that every user can pick his favorite format.

poke
I like this idea, I have a question, in your example, where you have time() but say will be from DB, I am confused on that part, wouldn't the only thing that is saved with a user be the offset? Also the gmdate() function, in your example, that is also doing the addition/subtraction? thanks
jasondavis
Sorry, the `gmdate` was supposed to be a simple `date` (as explained in the text), also I forgot to multiply it by `60` (to make the offset in seconds) - fixed that now. The calculation is only done for the timestamp, so you change the timestamp in the date function to make it appear as if it was a different time. And with `time()` from database I meant, if you display posts or something, that have a date, you would usually store that as well in the database, so in those cases it comes from there ;)
poke
Oh I got it, I dunno why I didn't before but thanks this should work great
jasondavis
btw any tips on how to get a users offset to save, currently I have a dropdown list with GMT-11 to GMT+14 – I would like to make dropdown list that would somehow get there current time and show the current times in each
jasondavis
You can use JavaScript to get the offset from the browser or simply loop through offsets to generate the current time in multiple timezones and echo them out inside of option tags for your dropdown list.
poke