I have show times stored in all in Eastern and ID from timezone settings table. Timezone settings table has Timezone_id, GMT offset and the timezone Name.
GMT offset are stored like +6, -4, +3.5, -4.5, etc.
I am trying to write a query to pull the showtime converted to its original timezone. I am doing like this.
SELECT Date_format( CONVERT_TZ( CONVERT_TZ(A.START_TIME, '+00:00', '-5:00'),
'+00:00', CONCAT(B.GMT_OFFSET , ':00' )), '%Y-%m-%e %r:%i')
AS 'start_time' from shows A, tz_settings B AND <<JOINS>>;
What I am doing here is first to convert the time from ET to GMT and then applying the GMT offset.
START_TIME is in date format like '2012-4-23 10:15:00' Also, one more problem is converting formats like "3.5" to "3.30" to pass to Convert_TZ
EDIT: Table structure. Main fields.
shows: 'id', 'show_name', 'stat_time', 'tz_id'
tz_settings: 'tz_id', 'gmt_offset','tz_name'
Any ideas? Any other functions would help in the mix?
Don't really know what is the problem that you are having, but I just build 2 MYSQL tables with your structure:
MySQL Tables
TABLE shows ['id', 'show_name', 'start_time', 'tz_id']
TABLE tz_settings ['tz_id', 'gmt_offset','tz_name']
And with this MySQL query:
SELECT Date_format( CONVERT_TZ( CONVERT_TZ( A.START_TIME, '+00:00', '-5:00' ) , '+00:00', CONCAT( B.GMT_OFFSET, ':00' ) ) , '%Y-%m-%e %r:%i' ) AS 'start_time'
FROM shows A, tz_settings B
WHERE A.id =1
AND B.tz_id = A.tz_id
The result was as expected!
Regardless, You should be storing the GMT OFFSET with it's full format to simplify your query thus losing the CONCAT and improving performance. Ex.: instead of +5, store +05:00
Does this helps you in any way ?
EDITED TO INCLUDE MYSQL IF AND REPLACE
SELECT Date_format( CONVERT_TZ( CONVERT_TZ( A.STAT_TIME, '+00:00', '-5:00' ) , '+00:00', if( B.GMT_OFFSET LIKE '%.5', REPLACE( B.GMT_OFFSET, '.5', ':30' ) , CONCAT( B.GMT_OFFSET, ':00' ) ) ) , '%Y-%m-%e %r:%i' ) AS 'start_time'
FROM shows A, tz_settings B
WHERE A.`id` =1
AND B.`tz_id` = A.`tz_id`
So, if the stored GMT OFFSET as .5 on it, it will be replaced by :30, otherwise, it will append the :00 to the existent value. (Tested and returns the value as expected)