How to get date using timezone saved in options?

This code does not work correctly and returns time using timezone from computer and ignoring timezone stored in WordPress options:

date_default_timezone_set( get_option( 'timezone_string' ) );
$hours = date( 'H', strtotime( $timestamp ) );
date_default_timezone_set(@date_default_timezone_get());

WordPress saves timezone in options section but ‘timezone_string’ option in wp_options table is empty. Also the code generates warnings.

Read More

How to get date using timezone saved in options correctly?

Where does WordPress store an Timezone which can be edited in options section?

Related posts

1 comment

  1. I use this:

    $mytheme_timezone = get_option('timezone_string');
    date_default_timezone_set($mytheme_timezone);

    in my themes functions.php. For me this has worked without any warnings.

    I’ve also tested whether my script is in different timezone than php.ini:

     if (strcmp($mytheme_timezone, ini_get('date.timezone'))){
     echo 'Script timezone differs from ini-set timezone.';
    }
    else {
     echo 'Script timezone and ini-set timezone match.';
    }
    

    Please improve this answer if you have more insight.

Comments are closed.