Posted By

anmari on 08/04/18


Tagged

timezone wordpress gmtoffset


Versions (?)

wp timezone and gmt offset


 / Published in: PHP
 

See https://wordpress.stackexchange.com/questions/308238/how-to-check-for-wordpress-timezone-type

  1. <?php
  2. function set_defaults_for_datetime() { // try for timezone string, else plan a plan with offset
  3.  
  4. if (($tz_text = get_option ('timezone_string') ) and (!empty($tz_text))) {
  5. $tz = timezone_open($tz_text);
  6. }
  7. else {
  8. if (($gmt_offset = get_option ('gmt_offset')) and (!(is_null($gmt_offset))) and (is_numeric($gmt_offset))) {
  9. $tz_text = $this->guess_timezone_from_offset($gmt_offset);
  10. $tz = timezone_open($tz_text );
  11. }
  12. else {
  13. $tz = timezone_open(date_default_timezone_get()); // this will give UTC as wordpress ALWAYS uses UTC
  14. }
  15. }
  16. return $tz;
  17. }
  18.  
  19.  
  20.  
  21.  
  22.  
  23. function guess_timezone_from_offset($offset) { // Because http://php.net/manual/en/datetimezone.construct.php requires a timezone string
  24. // Not all 1/2 hour gmt offset have matching timezones http://us.php.net/manual/en/timezones.others.php
  25. $timezones = calc_offset_from_tz_list ();
  26. // better to use the hardcoded way:
  27. $timezones = array(
  28. '-12'=>'Pacific/Kwajalein', // calculated way does not find this, it's actually +12
  29. '-11:30'=>'Pacific/Samoa', // doesn't exist https://en.wikipedia.org/wiki/UTC%E2%88%9211:30
  30. '-11'=>'Pacific/Samoa',
  31. '-10:30'=>'Pacific/Honolulu', //https://en.wikipedia.org/wiki/UTC%E2%88%9210:30
  32. '-10'=>'Pacific/Honolulu',
  33. '-9:30'=>'Pacific/Marquesas', //https://en.wikipedia.org/wiki/UTC%E2%88%9209:30
  34. '-9'=>'America/Juneau',
  35. '-8:30'=>'Pacific/Pitcairn', //pitcairn islands https://en.wikipedia.org/wiki/UTC%E2%88%9208:30
  36. '-8'=>'America/Los_Angeles',
  37. '-7:30'=>'America/Denver', // does not exist https://en.wikipedia.org/wiki/UTC%E2%88%9207:30
  38. '-7'=>'America/Denver',
  39. '-6:30'=>'America/Mexico_City', //does not exist https://en.wikipedia.org/wiki/UTC%E2%88%9206:30
  40. '-6'=>'America/Mexico_City',
  41. '-5:30'=>'America/New_York', //doesn't exist https://en.wikipedia.org/wiki/UTC%E2%88%9205:30
  42. '-5'=>'America/New_York',
  43. '-4:30'=>'America/Caracas',
  44. '-4'=>'America/Manaus',
  45. '-3:30'=>'America/St_Johns',
  46. '-3'=>'America/Argentina/Buenos_Aires',
  47. '-2:30'=>'America/St_Johns', //https://en.wikipedia.org/wiki/UTC%E2%88%9202:30
  48. '-2'=>'Brazil/DeNoronha',
  49. '-1:30'=>'Atlantic/Azores', // doesn't exist
  50. '-1'=>'Atlantic/Azores',
  51. '-0:30'=>'Europe/London', // doesn't exist
  52. '0'=>'Europe/London',
  53. '0:30'=>'Europe/London', //approx //https://en.wikipedia.org/wiki/UTC%2B00:30
  54. '1:30'=>'Africa/Johannesburg', // approx - very old https://en.wikipedia.org/wiki/UTC%2B01:30
  55. '1'=>'Europe/Paris',
  56. '2'=>'Europe/Helsinki',
  57. '2:30'=>'Europe/Moscow', //old
  58. '3'=>'Europe/Moscow',
  59. '3:30'=>'Asia/Tehran',
  60. '4'=>'Asia/Baku',
  61. '4:30'=>'Asia/Kabul',
  62. '5'=>'Asia/Karachi',
  63. '5:30'=>'Asia/Calcutta',
  64. '5:45'=>'Asia/Katmandu',
  65. '6'=>'Asia/Colombo',
  66. '6:30'=>'Asia/Rangoon',
  67. '7'=>'Asia/Bangkok',
  68. '7:30'=>'Asia/Singapore', // old
  69. '8'=>'Asia/Singapore',
  70. '8:30' => 'Asia/Harbin', //approx, was North KOrea etc
  71. '8:45' => 'Australia/Eucla',
  72. '9'=>'Asia/Tokyo',
  73. '9:30'=>'Australia/Darwin',
  74. '10'=>'Pacific/Guam',
  75. '10:30' => 'Australia/Lord_Howe',
  76. '11'=>'Australia/Sydney',
  77. '11:30'=>'Pacific/Norfolk',
  78. '12'=>'Asia/Kamchatka',
  79. '12:45' => 'Pacific/Chatham', //https://en.wikipedia.org/wiki/UTC%2B12:45
  80. '13'=>'Pacific/Enderbury',
  81. '13:45' => 'Pacific/Chatham', // https://en.wikipedia.org/wiki/UTC%2B13:45
  82. '14'=>'Pacific/Kiritimati'
  83. );
  84. $intoffset = intval($offset); /* to cope with +01.00 */
  85. $stroffset = strval($intoffset);
  86. if (isset($timezones[$stroffset]))
  87. return ($timezones[$stroffset]);
  88. else
  89. return false;
  90. }
  91.  
  92.  
  93.  
  94. function calc_offset_from_tz_list () {
  95. /* for occasional use - the calculated way to check things BUT have to hardcode some because wordpress allows gtm offsets that cannot easily be back calculated from the timezone list */
  96. $tzs = timezone_identifiers_list();
  97. //echo '<pre>'.print_r($tzs, true).'</pre>';
  98. $utc = new DateTimeZone('UTC');
  99. $now = new DateTime("now", $utc);
  100. foreach ($tzs as $tz) {
  101. $tzobj = new DateTimeZone($tz);
  102. $off = $tzobj->getOffset($now)/(60*60);
  103. $hour = (int) $off;
  104. $decimal = $off - $hour;
  105. $partial_hour = $decimal * 60;
  106. $offsets[$tz] = (string) $hour ;
  107. if ($partial_hour > 0) $offsets[$tz] .= ':'. (string) $partial_hour ;
  108. }
  109.  
  110. //echo '<pre>'.print_r( $offsets, true).'</pre>';
  111. $tzbyoffset =array_flip($offsets); // only latest will show
  112. ksort($tzbyoffset);
  113. echo '<pre>'.print_r( $tzbyoffset, true).'</pre>';
  114. return $tzbyoffset;
  115. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: anmari on August 4, 2018

This code fetches timezone string in wordpress option and if that does not exist, then gmt offset. If a gmtoffset is found, it attempts to assign a timezone string to it. There is also a check function which works out a gmt offset lookup from the php timezone list. WP allows gmt offsets that may not have a timezone assigned with that offset. Hardcoded best guesses are offered with links to sources.

You need to login to post a comment.