[wp-trac] [WordPress Trac] #24730: Introduce a timezone-retrieval method

WordPress Trac noreply at wordpress.org
Fri Jun 29 13:08:33 UTC 2018


#24730: Introduce a timezone-retrieval method
-------------------------+------------------------------
 Reporter:  rmccue       |       Owner:  (none)
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Date/Time    |     Version:
 Severity:  normal       |  Resolution:
 Keywords:  has-patch    |     Focuses:
-------------------------+------------------------------

Comment (by remcotolsma):

 {{{
 DateTime::setTimezone(): Can only do this for zones with ID for now
 }}}

 It looks like this issue is only active in PHP `< 5.4.26` or `> 5.5 <
 5.5.10`:
 https://3v4l.org/mlZX7

 Fixed it like this: https://github.com/pronamic/wp-
 datetime/blob/develop/src/DateTime.php

 {{{#!php
 <?php

         ...

         public function get_local_date() {
                 $wp_timezone = DateTimeZone::get_default();

                 /**
                  * PHP BUG: DateTime::setTimezone(): Can only do this for
 zones with ID for now.
                  * PHP version < 5.4.26
                  * PHP version > 5.5 < 5.5.10
                  *
                  * @link https://bugs.php.net/bug.php?id=45543
                  * @link https://3v4l.org/mlZX7
                  */
                 if ( version_compare( PHP_VERSION, '5.4.26', '<' ) || (
 version_compare( PHP_VERSION, '5.5', '>' ) && version_compare(
 PHP_VERSION, '5.5.10', '<' ) ) ) {
                         return new DateTime( date( self::MYSQL,
 $this->get_wp_timestamp() ), $wp_timezone );
                 }

                 $date = clone $this;
                 $date->setTimezone( $wp_timezone );

                 return $date;
         }

         ...

 }}}

 > I am not sure what you mean by "preventing formatting". If `date_i18n()`
 doesn't process timezone then output is incorrect since `date()` will
 assume UTC.

 We use a custom function that converts the timezone format characters:
 https://github.com/pronamic/wp-datetime/blob/develop/src/DateTime.php

 That way the `'timezone_string'` option is not used and we don't have to
 filter on `'pre_option_timezone_string'`.

 Ontopic: I think implementing `wp_timezone` is possible, but if
 plugin/theme developers start using this for `DateTime::setTimezone` on
 PHP `< 5.4.26` or `> 5.5 < 5.5.10` it could result in PHP warnings if an
 offset timezone is used. But maybe you can use some of this information in
 your `wp_date` work.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/24730#comment:13>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list