How to check for WordPress timezone type?

If we want to work with php’s datetime features and timezone objects AND handle sites that use a gmt_offset, sadly it does require some trickery. I have not found a better way to do it than this:

  1. Check for timezone string first – got one – great!
  2. Else Check for gmt offset, then try to assign best timezone string for that gmt_offset. This is a real guess, whatever we guess, may be wrong for some sites, especially sites that use a GMT offset but actually also have daylight saving in their area. They will be ‘wrong’ half the time.

Doing the timezone guess: One would think one could back calculate this from the php timezome identifiers list. HOWEVER wp allows gmtoffset that do NOT exist in the timezone list, sometimes these are every ‘old’. So some hardcoding is required.

3 functions provided below:

  1. code to try for both options and call ‘guess’ if necessary
  2. code to do hardcoded timezone lookup to cover all wordpress gmt offset possibilities (links in comments to explain why that tz string was chosen)
  3. Code to backcalculate from the php olson db list to crosscheck every now and then what php thinks the offset are.

https://snipplr.com/view/328819/wp-timezone-and-gmt-offset/

Leave a Comment