wc-functions.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. if( !function_exists('wp_get_timezone_string') ) {
  3. /**
  4. * Returns the timezone string for a site, even if it's set to a UTC offset
  5. *
  6. * Adapted from http://www.php.net/manual/en/function.timezone-name-from-abbr.php#89155
  7. *
  8. * @return string valid PHP timezone string
  9. */
  10. function wp_get_timezone_string() {
  11. // if site timezone string exists, return it
  12. if ( $timezone = get_option( 'timezone_string' ) )
  13. return $timezone;
  14. // get UTC offset, if it isn't set then return UTC
  15. if ( 0 === ( $utc_offset = get_option( 'gmt_offset', 0 ) ) )
  16. return 'UTC';
  17. // adjust UTC offset from hours to seconds
  18. $utc_offset *= 3600;
  19. // attempt to guess the timezone string from the UTC offset
  20. if ( $timezone = timezone_name_from_abbr( '', $utc_offset, 0 ) ) {
  21. return $timezone;
  22. }
  23. // last try, guess timezone string manually
  24. $is_dst = date( 'I' );
  25. foreach ( timezone_abbreviations_list() as $abbr ) {
  26. foreach ( $abbr as $city ) {
  27. if ( $city['dst'] == $is_dst && $city['offset'] == $utc_offset )
  28. return $city['timezone_id'];
  29. }
  30. }
  31. // fallback to UTC
  32. return 'UTC';
  33. }
  34. }