I am storing dates in a MySQL database in datetime fields in UTC. I\'m using PHP, and I\'ve called date_timezone_set(\'UTC\') so that all calls to date() (without timestamp)
What you're doing is the right way of doing things. I'd recommend sticking with working in only UTC and just converting at the last minute for the display.
Here's a quick function I put together for time zone conversion using the DateTime class that comes with PHP. It's a bit more code than you have but I think it's easier and a better way to structure things...
function convert_time_zone($date_time, $from_tz, $to_tz)
{
$time_object = new DateTime($date_time, new DateTimeZone($from_tz));
$time_object->setTimezone(new DateTimeZone($to_tz));
return $time_object->format('Y-m-d H:i:s');
}
http://richardwillia.ms/blog/2011/04/time-zone-conversion-using-datetime-class/
Hope that helps.
Having spent a lot of time dealing with this issue, do not attempt to implement time zone translation yourself. It's a royal PIA, fraught with difficulties, and it's very hard to get it right internationally.
That said, the best option is to convert your datetimes in MySQL to timestamps, and just use the database to convert times:
mysql> set time_zone='America/New_York';
timestamps in MySQL are smaller, and support time zone translation. datetime does not.
Before you display the site information on the page, just invoke the above command, and it will display correctly without any PHP code changes at all.
Caveats:
To turn off timestamp properties:
ALTER TABLE mytable CHANGE COLUMN Created Created timestamp NULL DEFAULT 0;
The DEFAULT 0 disables the column being updated when you update other columns.