0

If you choose a city as a timezone it will save to the "timezone_string" option but if you choose a Manual Offset (UTC+1 for instance) it is saved to the "gmt_offset" option

if you need the gmt_offset then

<?php echo get_option('gmt_offset'); ?>

this will give you an integer like 2 or -2.

and if you need the timezone string use

<?php echo get_option('timezone_string'); ?>

this will give you a string like America/Indianapolis


But i the php code like this

  $tzs = get_option('gmt_offset');
  $tzobj = timezone_open($tzs);
  $now = date_create('now',$tzobj );

this will give me what day is today

$todaywppl=date_format($now, 'l');

when when theme is activated the wordpress defaults to UTC+0 because of which the $tzs will throw warning like

timezone_open(): Unknown or bad timezone (0)

What i am trying to get is current day ..i.e Monday Tuesday like that. It worked fine for me if i am setting America/Indianapolis - a string.

The discussion on this was done here -> Wordpress returns a wrong date

How can i fix this? Can i make it work on both way?

1

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.