WordPress.org

Make WordPress Core

Ticket #16920: get_gmt_from_date-php5.diff

File get_gmt_from_date-php5.diff, 1.7 KB (added by technosailor, 9 years ago)

get rid of php4 compat for get_gmt_from_date()/ with unit test

  • wp-includes/formatting.php

     
    15881588 *
    15891589 * Requires and returns a date in the Y-m-d H:i:s format. Simply subtracts the
    15901590 * value of the 'gmt_offset' option. Return format can be overridden using the
    1591  * $format parameter. If PHP5 is supported, the function uses the DateTime and
    1592  * DateTimeZone objects to respect time zone differences in DST.
     1591 * $format parameter.
    15931592 *
    15941593 * @since 1.2.0
    15951594 *
     
    16011600function get_gmt_from_date($string, $format = 'Y-m-d H:i:s') {
    16021601        preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
    16031602        $tz = get_option('timezone_string');
    1604         if( class_exists('DateTime') && $tz ) {
    1605                 //PHP5
    1606                 date_default_timezone_set( $tz );
    1607                 $datetime = new DateTime( $string );
    1608                 $datetime->setTimezone( new DateTimeZone('UTC') );
    1609                 $offset = $datetime->getOffset();
    1610                 $datetime->modify( '+' . $offset / 3600 . ' hours');
    1611                 $string_gmt = gmdate($format, $datetime->format('U'));
    1612 
    1613                 date_default_timezone_set('UTC');
    1614         }
    1615         else {
    1616                 //PHP4
    1617                 $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
    1618                 $string_gmt = gmdate($format, $string_time - get_option('gmt_offset') * 3600);
    1619         }
     1603        date_default_timezone_set( $tz );
     1604        $datetime = new DateTime( $string );
     1605        $datetime->setTimezone( new DateTimeZone('UTC') );
     1606        $offset = $datetime->getOffset();
     1607        $datetime->modify( '+' . $offset / 3600 . ' hours');
     1608        $string_gmt = gmdate($format, $datetime->format('U'));
     1609        date_default_timezone_set('UTC');
    16201610        return $string_gmt;
    16211611}
    16221612