[wp-trac] [WordPress Trac] #39595: date_default_timezone_set in wp-settings.php is set prematurely and ignorantly, regardless of other defined settings

WordPress Trac noreply at wordpress.org
Sun Jan 15 20:41:35 UTC 2017


#39595: date_default_timezone_set in wp-settings.php is set prematurely and
ignorantly, regardless of other defined settings
--------------------------+-----------------------------
 Reporter:  mkormendy     |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Date/Time     |    Version:  4.7.1
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 I've been having issues in WordPress with my logs and my plugins reporting
 either UTC or local time, or incorrect offsets, but not one consistent
 time code as selected in Settings > General setting in the WP Admin.

 Realizing that server configurations are difficult to corral for
 consistency, some simple checks should be performed and reported to the
 user before adjusting their timezone and dates in WordPress.

 This could have serious effects to many plugins as well, depending on what
 they previously expected, and what actually *should* happen.

 == First, some background... ==

 The operating system (CentOS 7) reports this:

 {{{
 [mike at myserver ~]# timedatectl
       Local time: Sat 2017-01-14 21:31:47 CST
   Universal time: Sun 2017-01-15 03:31:47 UTC
         RTC time: Sun 2017-01-15 09:10:47
        Time zone: America/Chicago (CST, -0600)
      NTP enabled: yes
 NTP synchronized: yes
  RTC in local TZ: no
       DST active: no
  Last DST change: DST ended at
                   Sun 2016-11-06 01:59:59 CDT
                   Sun 2016-11-06 01:00:00 CST
  Next DST change: DST begins (the clock jumps one hour forward) at
                   Sun 2017-03-12 01:59:59 CST
                   Sun 2017-03-12 03:00:00 CDT
 }}}

 My Apache `httpd.conf` has this setting (with successful expectations):

 {{{
 SetEnv TZ America/Chicago
 }}}

 My PHP `php.ini` file has this setting (with successful expectations):

 {{{
 [Date]
 date.timezone = America/Chicago
 }}}

 And with PHP CLI, when I run the following command, PHP reports this:

 {{{
 [mike at myserver ~]# php -a
 Interactive shell

 php > echo date_default_timezone_get(time());
 America/Chicago
 }}}

 Because MySQL naturally bases its timezone/offset settings on the
 operating system (I will ignore any sort of timezone database
 configurations).
 When I log into MySQL via the command line and run the following query at
 the current time of 9:43pm on January 14, 2017, I get this (with
 successful expectations):

 {{{
 mysql> SELECT NOW();

 2017-01-14 21:43:26
 }}}

 This verifies that my setup is configured properly on the backend.

 == Now to test the WordPress PHP code... ==

 First, I would like to have WordPress operate as UTC-6 (or
 America/Chicago) time.
 I go into the WP Admin Settings > General page and set the timezone to
 'UTC-6'.

 Next, I make sure all plugins are deactivated.

 In the root 2017 theme, I have gone into my functions.php file to create
 some debugging flags. I have simply added this line to my functions file:

 {{{
 error_log('Get reported WordPress GMT offset: ' .
 get_option('gmt_offset'));
 error_log('Get reported WordPress timezone: ' .
 get_option('timezone_string'));
 error_log('Get reported PHP timezone: ' . date_default_timezone_get());
 }}}

 I turn on my debugging in my `wp-config.php` file and look to my logs to
 see what is outputted.

 {{{
 [mike at myserver ~]# tail -f -n 300/var/www/www.myserver.com/public_html/wp-
 content/debug.log
 }}}

 After loading the website homepage a few times, I see a series of entries:

 {{{
 [15-Jan-2017 04:03:31 UTC] Get reported WordPress GMT offset: -6
 [15-Jan-2017 04:03:31 UTC] Get reported WordPress timezone:
 [15-Jan-2017 04:03:31 UTC] Get reported PHP timezone: UTC
 [15-Jan-2017 04:03:31 UTC] Get reported WordPress GMT offset: -6
 [15-Jan-2017 04:03:31 UTC] Get reported WordPress timezone:
 [15-Jan-2017 04:03:31 UTC] Get reported PHP timezone: UTC
 }}}

 The option for GMT offset is set to -6 as expected.
 **However,** the preliminary timestamp shows a time in UTC and PHP still
 reports that something has set the timezone to UTC.

 Then I change my timezone settings in the WP Admin under Settings >
 General from 'UTC-6' to 'Chicago' (under the America select dropdown
 group). My debug log now has this:

 {{{
 [15-Jan-2017 04:12:35 UTC] Get reported WordPress GMT offset: -6
 [15-Jan-2017 04:12:35 UTC] Get reported WordPress timezone:
 America/Chicago
 [15-Jan-2017 04:12:35 UTC] Get reported PHP timezone: UTC
 [15-Jan-2017 04:12:35 UTC] Get reported WordPress GMT offset: -6
 [15-Jan-2017 04:12:35 UTC] Get reported WordPress timezone:
 America/Chicago
 [15-Jan-2017 04:12:35 UTC] Get reported PHP timezone: UTC
 }}}

 Okay so now both the GMT offset and America/Chicago show up as expected.
 **But again,** the timestampt and PHP report the timezone is still set to
 UTC.

 **Remember,** we set PHP's initialization to date.timezone =
 America/Chicago and verified that it was being reported by the PHP CLI
 outside of any other scripts running (WordPress or otherwise).

 This is misleading, as any further PHP operations WP methods or otherwise
 now operate in UTC time.
 This also has me wondering if the WP time/date related methods actually
 account for this in their operation. If I am using a plugin like Gravity
 Forms with its own custom tables, it may not use the traditional post
 time/date methods for its own records yet wish to still use the overall
 timezone setting in WordPress (a common and natural use case for many
 plugins).

 == Where exactly is WordPress forcing the timezone to UTC? ==

 I search from the base of my website folder through all of the PHP files
 recursively for the string text of the function that solely responsible
 for changing ANY timezone in PHP, namely: date_default_timezone_set(). My
 search is abstracted to a simple `date_default_timezone_set` string
 without the parenthesis to catch all instances of the string.

 Of the plugins (must-use or otherwise) that contain
 'date_default_timezone_set', I have completely disabled them and confirm
 they are not active

 The final output of searched files are the following:

 {{{
 /var/www/www.myserver.com/public_html/wp-
 settings.php:51:date_default_timezone_set( 'UTC' );
 /var/www/www.myserver.com/public_html/wp-admin/options-general.php:178:
 date_default_timezone_set($tzstring);
 /var/www/www.myserver.com/public_html/wp-admin/options-general.php:220:
 date_default_timezone_set('UTC');
 /var/www/www.myserver.com/public_html/wp-includes/class-
 phpmailer.php:3275:
 date_default_timezone_set(@date_default_timezone_get());
 }}}

 Notably, the first and the third items in my search stand out to me.

 == Altering the WordPress core to produce a change... ==

 The only change that seemed to make a lasting positive effect across the
 entire installation where expected was to comment out the following line
 in my wp-settings.php file (~line 51):

 {{{
 // WordPress calculates offsets from UTC.
 date_default_timezone_set( 'UTC' );
 }}}

 to:

 {{{
 // WordPress calculates offsets from UTC.
 // date_default_timezone_set( 'UTC' );
 }}}

 Refreshing and visiting pages in the browser, my logs are reporting the
 timestamp correctly now. As well, when I create a post, posts, custom
 post-types and debug logs are all reporting their time correctly now.

 {{{
 [15-Jan-2017 01:59:04 America/Chicago] Get reported WordPress GMT offset:
 -6
 [15-Jan-2017 01:59:04 America/Chicago] Get reported WordPress timezone:
 America/Chicago
 [15-Jan-2017 01:59:04 America/Chicago] Get reported PHP timezone:
 America/Chicago
 [15-Jan-2017 01:59:04 America/Chicago] Get reported WordPress GMT offset:
 -6
 [15-Jan-2017 01:59:04 America/Chicago] Get reported WordPress timezone:
 America/Chicago
 [15-Jan-2017 01:59:04 America/Chicago] Get reported PHP timezone:
 America/Chicago
 }}}

 The only thing that is reporting incorrectly is the output facade
 timestamps in the WP Admin under Settings > General:

 `Universal time (UTC) is <em>2017-01-15 02:12:06</em>. Local time is
 <em>2017-01-14 20:12:06</em>.`

 The Universal time is actually displaying what is a time matching my local
 time, and the local time is 12 hours behind universal time (which should
 be only 6 hours behind).

 == Conclusion... ==

 I'm not sure there is something that should be done, or if I am simply
 attempting to prove an issue with an edge case (which I highly doubt).

 Should the core WP code wp-settings.php file be altered so that it checks
 for the following first before setting a timezone?:

 * check for php.ini's `date.timezone = America/Chicago`
 * check for the values in the DB for both `gmt_offset` and
 `timezone_string`

 If there are changes that are valid that need to be made but possibly
 affect many plugins and themes in the future, maybe there could be a
 **'Legacy'** checkbox option in the WP Admin under Settings > General to
 allow people to use the old timezone settings as expected for their
 specific installation.

 For the core developers that are more familiar with the code, can you shed
 some light or feedback on this?

--
Ticket URL: <https://core.trac.wordpress.org/ticket/39595>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list