RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts</strong | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.69% (5.89%*) • Home Loan Fixed: 5.39% (6.59%*) • Fixed: 5.39% (6.59%*) • Variable: 5.69% (5.89%*) • Investment IO: 5.69% (6.48%*) • Investment PI: 5.39% (6.59%*)

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 complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment