wc_timezone_string

WooCommerce Timezone - helper to retrieve the timezone string for a site until.

Description

(string) wc_timezone_string(); 

a WP core method exists (see https://core.trac.wordpress.org/ticket/24730).

Adapted from https://secure.php.net/manual/en/function.timezone-name-from-abbr.php#89155.

Returns (string)

PHP timezone string for the site


Usage

  1. if ( !function_exists( 'wc_timezone_string' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce/includes/wc-formatting-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wc_timezone_string(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/wc-formatting-functions.php  
  1. function wc_timezone_string() { 
  2.  
  3. // if site timezone string exists, return it 
  4. if ( $timezone = get_option( 'timezone_string' ) ) { 
  5. return $timezone; 
  6.  
  7. // get UTC offset, if it isn't set then return UTC 
  8. if ( 0 === ( $utc_offset = intval( get_option( 'gmt_offset', 0 ) ) ) ) { 
  9. return 'UTC'; 
  10.  
  11. // adjust UTC offset from hours to seconds 
  12. $utc_offset *= 3600; 
  13.  
  14. // attempt to guess the timezone string from the UTC offset 
  15. if ( $timezone = timezone_name_from_abbr( '', $utc_offset ) ) { 
  16. return $timezone; 
  17.  
  18. // last try, guess timezone string manually 
  19. foreach ( timezone_abbreviations_list() as $abbr ) { 
  20. foreach ( $abbr as $city ) { 
  21. if ( (bool) date( 'I' ) === (bool) $city['dst'] && $city['timezone_id'] && intval( $city['offset'] ) === $utc_offset ) { 
  22. return $city['timezone_id']; 
  23.  
  24. // fallback to UTC 
  25. return 'UTC';