diff options
author | Oleg Pudeyev <oleg@bsdpower.com> | 2011-03-10 19:14:53 -0500 |
---|---|---|
committer | Oleg Pudeyev <oleg@bsdpower.com> | 2012-03-14 22:57:31 -0400 |
commit | bb461c8daa97358e8bcce923a21eba0abd6ea3dc (patch) | |
tree | 471d8762b6dffca280306f8e03e8f50d6cf97fa3 /phpBB/includes/functions.php | |
parent | f17664a00c9fa3b80a887bde2cdb2424a5d5567a (diff) | |
download | forums-bb461c8daa97358e8bcce923a21eba0abd6ea3dc.tar forums-bb461c8daa97358e8bcce923a21eba0abd6ea3dc.tar.gz forums-bb461c8daa97358e8bcce923a21eba0abd6ea3dc.tar.bz2 forums-bb461c8daa97358e8bcce923a21eba0abd6ea3dc.tar.xz forums-bb461c8daa97358e8bcce923a21eba0abd6ea3dc.zip |
[feature/new-tz-handling] Sort timezones in selector by offset.
Since the list of timezones is very long, and users are likely
to know their current offset but not necessarily which city
that is nearby is in the timezone database, sort the list of
timezones by offset.
UTC is specially handled to show up before other GMT+0 timezones.
PHPBB3-9558
Diffstat (limited to 'phpBB/includes/functions.php')
-rw-r--r-- | phpBB/includes/functions.php | 71 |
1 files changed, 70 insertions, 1 deletions
diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index 69bfe3e090..31a191b513 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -1069,6 +1069,65 @@ function style_select($default = '', $all = false) return $style_options; } +function phpbb_format_timezone_offset($tz_offset) +{ + $sign = ($tz_offset < 0) ? '-' : '+'; + $time_offset = abs($tz_offset); + + $offset_seconds = $time_offset % 3600; + $offset_minutes = $offset_seconds / 60; + $offset_hours = ($time_offset - $offset_seconds) / 3600; + + $offset_string = sprintf("%s%02d:%02d", $sign, $offset_hours, $offset_minutes); + return $offset_string; +} + +// Compares two time zone labels. +// Arranges them in increasing order by timezone offset. +// Places UTC before other timezones in the same offset. +function tz_select_compare($a, $b) +{ + $a_sign = $a[3]; + $b_sign = $b[3]; + if ($a_sign != $b_sign) + { + return $a_sign == '-' ? -1 : 1; + } + + $a_offset = substr($a, 4, 5); + $b_offset = substr($b, 4, 5); + if ($a_offset == $b_offset) + { + $a_name = substr($a, 12); + $b_name = substr($b, 12); + if ($a_name == $b_name) + { + return 0; + } else if ($a_name == 'UTC') + { + return -1; + } else if ($b_name == 'UTC') + { + return 1; + } + else + { + return $a_name < $b_name ? -1 : 1; + } + } + else + { + if ($a_sign == '-') + { + return $a_offset > $b_offset ? -1 : 1; + } + else + { + return $a_offset < $b_offset ? -1 : 1; + } + } +} + /** * Pick a timezone * @todo Possible HTML escaping @@ -1083,7 +1142,17 @@ function tz_select($default = '', $truncate = false) { $timezones = DateTimeZone::listIdentifiers(); - sort($timezones); + foreach ($timezones as &$timezone) + { + $tz = new DateTimeZone($timezone); + $dt = new phpbb_datetime('now', $tz); + $offset = $dt->getOffset(); + $offset_string = phpbb_format_timezone_offset($offset); + $timezone = 'GMT' . $offset_string . ' - ' . $timezone; + } + unset($timezone); + + usort($timezones, 'tz_select_compare'); } $tz_select = ''; |