最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

woocommerce offtopic - Woocomerce pulling wrong time from server -5 hrs difference

programmeradmin2浏览0评论

I wonder could anyone help me with the time zones. In my wordpress i have the time zone set for New York, When i make a booking on my wordpress the booking is added to woocommerce cart and the time is totally changed by 5 hours differnece. I think i have found the correct file to edit in woocommerce wc-formatting-functions.php

/**
 * Convert mysql datetime to PHP timestamp, forcing UTC. Wrapper for strtotime.
 *
 * Based on wcs_strtotime_dark_knight() from WC Subscriptions by Prospress.
 *
 * @since  3.0.0
 *
 * @param string $time_string
 * @param int|null $from_timestamp
 *
 * @return int
 */
function wc_string_to_timestamp( $time_string, $from_timestamp = null ) {
    $original_timezone = date_default_timezone_get();

    // @codingStandardsIgnoreStart
    date_default_timezone_set( 'UTC' );

    if ( null === $from_timestamp ) {
        $next_timestamp = strtotime( $time_string );
    } else {
        $next_timestamp = strtotime( $time_string, $from_timestamp );
    }

    date_default_timezone_set( $original_timezone );
    // @codingStandardsIgnoreEnd

    return $next_timestamp;
}

or maybe this part

function wc_timezone_string() {

    // if site timezone string exists, return it
    if ( $timezone = get_option( 'timezone_string' ) ) {
        return $timezone;
    }

    // get UTC offset, if it isn't set then return UTC
    if ( 0 === ( $utc_offset = intval( get_option( 'gmt_offset', 0 ) ) ) ) {
        return 'UTC';
    }

    // adjust UTC offset from hours to seconds
    $utc_offset *= 3600;

    // attempt to guess the timezone string from the UTC offset
    if ( $timezone = timezone_name_from_abbr( '', $utc_offset ) ) {
        return $timezone;
    }

    // last try, guess timezone string manually
    foreach ( timezone_abbreviations_list() as $abbr ) {
        foreach ( $abbr as $city ) {
            if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) {
                return $city['timezone_id'];
            }
        }
    }

    // fallback to UTC
    return 'UTC';
}

I wonder could anyone help me with the time zones. In my wordpress i have the time zone set for New York, When i make a booking on my wordpress the booking is added to woocommerce cart and the time is totally changed by 5 hours differnece. I think i have found the correct file to edit in woocommerce wc-formatting-functions.php

/**
 * Convert mysql datetime to PHP timestamp, forcing UTC. Wrapper for strtotime.
 *
 * Based on wcs_strtotime_dark_knight() from WC Subscriptions by Prospress.
 *
 * @since  3.0.0
 *
 * @param string $time_string
 * @param int|null $from_timestamp
 *
 * @return int
 */
function wc_string_to_timestamp( $time_string, $from_timestamp = null ) {
    $original_timezone = date_default_timezone_get();

    // @codingStandardsIgnoreStart
    date_default_timezone_set( 'UTC' );

    if ( null === $from_timestamp ) {
        $next_timestamp = strtotime( $time_string );
    } else {
        $next_timestamp = strtotime( $time_string, $from_timestamp );
    }

    date_default_timezone_set( $original_timezone );
    // @codingStandardsIgnoreEnd

    return $next_timestamp;
}

or maybe this part

function wc_timezone_string() {

    // if site timezone string exists, return it
    if ( $timezone = get_option( 'timezone_string' ) ) {
        return $timezone;
    }

    // get UTC offset, if it isn't set then return UTC
    if ( 0 === ( $utc_offset = intval( get_option( 'gmt_offset', 0 ) ) ) ) {
        return 'UTC';
    }

    // adjust UTC offset from hours to seconds
    $utc_offset *= 3600;

    // attempt to guess the timezone string from the UTC offset
    if ( $timezone = timezone_name_from_abbr( '', $utc_offset ) ) {
        return $timezone;
    }

    // last try, guess timezone string manually
    foreach ( timezone_abbreviations_list() as $abbr ) {
        foreach ( $abbr as $city ) {
            if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) {
                return $city['timezone_id'];
            }
        }
    }

    // fallback to UTC
    return 'UTC';
}
Share Improve this question edited Nov 8, 2017 at 11:27 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Nov 8, 2017 at 4:33 Ryan Mc GonagleRyan Mc Gonagle 315 bronze badges 2
  • Internally, WordPress timestamp are stored in UTC format. What is the code you use to read the date ? – mmm Commented Nov 8, 2017 at 8:36
  • i have just posted the code that woocommerce uses for the date. I am using two plugins Bookly which pulls the time from the wordpress time zone and wocommerce which seems to pull the time from the server and not the wordpress time zone – Ryan Mc Gonagle Commented Nov 8, 2017 at 9:22
Add a comment  | 

1 Answer 1

Reset to default 1

For the date Woocommerce CODE

function wc_string_to_datetime( $time_string ) {
    // Strings are defined in local WP timezone. Convert to UTC.
    if ( 1 === preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|((-|\+)\d{2}:\d{2}))$/', $time_string, $date_bits ) ) {
        $offset    = ! empty( $date_bits[7] ) ? iso8601_timezone_to_offset( $date_bits[7] ) : wc_timezone_offset();
        $timestamp = gmmktime( $date_bits[4], $date_bits[5], $date_bits[6], $date_bits[2], $date_bits[3], $date_bits[1] ) - $offset;
    } else {
        $timestamp = wc_string_to_timestamp( get_gmt_from_date( gmdate( 'Y-m-d H:i:s', wc_string_to_timestamp( $time_string ) ) ) );
    }
    $datetime  = new WC_DateTime( "@{$timestamp}", new DateTimeZone( 'UTC' ) );

    // Set local timezone or offset.
    if ( get_option( 'timezone_string' ) ) {
        $datetime->setTimezone( new DateTimeZone( wc_timezone_string() ) );
    } else {
        $datetime->set_utc_offset( wc_timezone_offset() );
    }

    return $datetime;
}
发布评论

评论列表(0)

  1. 暂无评论