Possible Duplicate:
Datatype/structure to store timezones in MySQL
I'll be storing only UTC times in my database, and want to convert the timestamps to a user's local time using PHP.
My question is, what is the best way to store the users timezone in the database?
IE: -400, -4, -4:00
With that said, I know how to do the math manually:
$utc_time = strtotime("2011-10-02 23:00:00");
$my_time = $utc_time - 14400;
echo date($timeformat, $mytime);
My problem is, with the user's timezone being pulled from the database, I don't know whether I'll be adding or subtracting seconds. That's where I run into the problem of trying to figure out how to save the timezone so that I can calculate the offset.
I can save the timezone as "-14400", but using the above example, I can't just combine the two strings to do the math for me using the - sign:
$my_time = $utc_time.$timezone;
So... how do I save the timezone properly to get the math done?
The timezone in one location isn't always the same - for example, the UK is BST (GMT + 1) between March and October. Use one of the timezones supported by PHP:
http://php.net/manual/en/timezones.php
If you do go ahead using numbers, either store them as hours or minutes. Store timezones west of UTC/GMT as negative numbers. For example, the US East Coast would be -5 (hours) or -300 (minutes) - assuming it's 5 hours behind.
Then, add this to the timestamp - the negative or positive will handle the rest.
// for 5 hours behind when stored as hours (-5)
$now = time() + ($offset * 60 * 60);
// for 5 hours behind when stored as minutes (-300)
$now = time() + ($offset * 60);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With