Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggregation Enquires Welcome

Example Interest Rates: Home Loan Variable: 1.89% (1.9%*) • Home Loan Fixed: 1.84% (2.11%*) • Fixed: 1.84% (2.11%*) • Variable: 1.89% (1.9%*) • Investment IO: 2.05% (4.36%*) • Investment PI: 1.58% (2.72%*)

Get Global Timezones/Locations in Dropdown Select Menu

Using PHP's listidentifiers (part of the DateTimeZone class ), we can create a dropdown menu of all available global timezone identifiers. It's handy for inclusion in forms if you're after a general region rather than just country.

The first function retrieves timezone values and returns an array:

1
<?php 
2
/*
3
 Get Global Timezones/Locations in Dropdown Select Menu
4
 http://www.beliefmedia.com/code/php-snippets/timezone-select-menu
5
*/
6
 
7
function beliefmedia_get_timezones() {
8
  $o = array();
9
  $t_zones = timezone_identifiers_list();
10
 
11
  foreach($t_zones as $a) {
12
  $t = '';
13
 
14
    try {
15
      /* Throws exception for 'US/Pacific-New' */
16
      $zone = new DateTimeZone($a);
17
 
18
      $seconds = $zone->getOffset( new DateTime('now', $zone) );
19
      $hours = sprintf( '%+02d', intval($seconds / 3600));
20
      $minutes = sprintf( '%02d', ($seconds % 3600) / 60);
21
 
22
      $t = $a . '  [ ' . $hours . ':' . $minutes . ' ]';
23
      $o[$a] = $t;
24
    }
25
 
26
    /* Exceptions must be catched, else a blank page */
27
    catch(Exception $e) {
28
      /* die("Exception : " . $e->getMessage() . ''); */
29
    }
30
 
31
  }
32
 
33
  /* Sort array by key */
34
  ksort($o);
35
 
36
 return $o;
37
}

The second function retrieves the timezone array and manufactures the select menu.

1
<?php 
2
/*
3
 Get Global Timezones/Locations in Dropdown Select Menu
4
 http://www.beliefmedia.com/code/php-snippets/timezone-select-menu
5
*/
6
 
7
function beliefmedia_timezones() {
8
 
9
  /* Get timezones */
10
  $o = beliefmedia_get_timezones();
11
 
12
  foreach($o as $tz => $label) {
13
    $return .= '<option value="' . $tz . '">' . $label . '</option>';
14
  }
15
 
16
 return '<select name="timezone" style="height: 30px; font-size: 1.0em;">' . $return . '</select>';
17
}

Usage is as follows:

/* Usage */
echo beliefmedia_timezones();

The results is as follows:

Note: I've included raw HTML to show the list so timezone values may not be correct.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  AUS Eastern Standard Time (Connecticut)

  Want to have a chat?
 

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment