<?php /** * Display a timezone input */ namespace Events\UI; use Events\API\Util; elgg_load_js('events/timezone'); $name = elgg_extract('name', $vars, 'timezone'); $value = elgg_extract('value', $vars, Util::getClientTimezone()); $this_timezone = Util::getTimezoneInfo($value); $this_country_code = $this_timezone->country_code; $timezones = Util::getTimezonesByCountry(); $country_options = array(); $timezone_options = array(); foreach ($timezones as $country_code => $country_timezones) { $country_options[$country_code] = elgg_echo("timezone:country:{$country_code}"); if ($country_code == $this_country_code) { foreach ($country_timezones as $country_timezone) { $timezone_options[$country_timezone->id] = $country_timezone->label; } } } asort($country_options); ?> <div class="elgg-input-timezone clearfix"> <div class="elgg-col elgg-col-1of3"> <?php echo elgg_view('input/dropdown', array('data-timezone-country' => $this_country_code, 'value' => $this_country_code, 'options_values' => $country_options)); ?>
/** * Returns an array of timezones by country * @return array */ public static function getTimezonesByCountry() { $timezones = array(); $tz_ids = array_keys(self::getTimezones(true, false, 'now', self::TIMEZONE_SORT_OFFSET)); foreach ($tz_ids as $tz_id) { if ($tz_id == Util::UTC) { continue; } $info = Util::getTimezoneInfo($tz_id); $cc = $info->country_code; $abbr = $info->abbr; if (!isset($timezones[$cc])) { $timezones[$cc] = array(); } $timezones[$cc][] = $info; } ksort($timezones); return $timezones; }