PHP Echo Timezone based on GMT offset

dateoffsetPHPtimezone

I have an application that stores GMT time data in a MySQL db like this: 2009-12-16 15:27:47.

The user's timezone offset is stored in another column like this: -6

The above timezone is GMT-6, which is CST. Is there any way I can echo CST (i.e. – date('T")) from the GMT offset data?

Note: I should also add that the application global timezone is set using: date_default_timezone_set('GMT');

Best Answer

Unfortunately, there isn't always a single timezone for each GMT offset. Many GMT offsets have multiple timezones. Also, GMT offsets change during DST (where applicable). For instance, during DST, US/Arizona and US/Mountain share the same GMT offset; however, during the rest of the year, US/Mountain and US/Arizona don't share the same GMT offset. You can either use DateTimeZone::listIdentifiers() or check out the manual for a list of supported timezones in PHP.

If you were starting an application from scratch, I would suggest storing the timezone identifier instead of the GMT offset, but I'm guessing your application is already in production. In that case, there's not really a reliable way to display the timezone without improving your data.

I would suggest that you add a timezone column to the table. You could write a one-time script that would set the timezone to some pre-defined list of defaults based on the GMT offset. For instance, if the GMT offset is 8, set the timezone to America/Los_Angeles. Finally, change your application to display/save timezone identifiers instead of GMT offsets. This may cause problems for a subset of your users, but it allows them to change their timezone if they need to.