I am developing an API using php where I just send out the query result as JSON string. The result is combination of multiple tables. However, one of the field is Time. I need to convert this to a different timezones based on the timezone setting from a different table.
Here is the current output:
Array ( "Show_name" => "Billards",
"Show_type" => "Movie",
"Start_Time" => "2011-11-09 07:00:00",
"End_Time" => "2011-11-09 09:00:00",
"Open" => "Yes"
);
Start time and end time are in EST for all records by default. Timezone settings are stored in a different table with fields Timezone_ID, GMT_offset, DST_offset. Based on these settings start_time and end_time, which are in est by default, should be converted to respective timezones.
I was initially planning to write a MySQL function to calculate this right from the query itself, that way the JSON format / fields do not change. But I see that, its way too slow compared to PHP function.
What do you suggest based on your experience?
– Is there a way to simply calculate with in the query?
– Does MySQL functions perform better than PHP?
– If I need to go with php function, I need to loop through all records to calculate this. Not sure if I need to pull the timezone record separately. Because I don’t want to change the format / number of fields in the JSON response.
Any other better way?
I generally do this on the database side. Note that I store all dates in the database as UTC.
So, to display data to the user in the user’s local time zone, I do something like this:
Where ‘EST’ is the timezone that the user has selected in their preferences.
Changing your dates in the database from EST to UTC does not require any schema change, as the timezone is not stored in the schema.
To convert all your dates from EST to UTC, simply do this: