}
$ce = DB_DataObject::factory('core_enum');
- $ce->query('
+ $ce->query("
SELECT
- *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, "UTC")), "%H:%i") as timeOffset
+ *, TIME_FORMAT(TIMEDIFF(NOW(), CONVERT_TZ(NOW(), Name, 'UTC')), '%H:%i') as timeOffset
FROM
mysql.time_zone_name
+ WHERE
+ Name LIKE '%/%'
+ AND
+ Name NOT LIKE '%/%/%'
+ AND
+ Name NOT LIKE 'Etc%'
ORDER BY
timeoffset DESC,
Name DESC
- ');
+ ");
while($ce->fetch()) {
// ignroe timezone such as 'CET' and 'America/Argentina/Buenos_Aires'
- if(substr_count($ce->Name, '/') != 1) {
- continue;
- }
+
$ar = explode('/', $ce->Name);
// ignore timezone such as 'Etc/GMT+8'
- if($ar[0] == 'Etc') {
- continue;
- }
+
+
+ $displayArea = str_replace('_', ' ', $ar[1]);
$timeOffset = ((substr($ce->timeOffset, 0, 1) == '-') ? '' : '+') . $ce->timeOffset;
- $displayOffset = '(GMT ' . ((substr($ce->timeOffset, 0, 1) == '-') ? '' : '+') . $ce->timeOffset . ')';
- $offsetAr = explode(':', $ce->timeOffset);
+ $displayOffset = '(GMT ' . $timeOffset . ')';
self::$timezones[$ce->Name] = array(
'region' => $ar[0],
'area' => $ar[1],
- 'displayName' => $ce->Name . ' ' . $displayOffset,
- 'displayArea' => $ar[1] . ' ' . $displayOffset,
- 'timeOffset' =>
- 'decimalOffset' => $offsetAr[0] + (($offsetAr[0] < 0) ? (-1 * $offsetAr[1] / 60) : ($offsetAr[1] / 60))
+ 'displayName' => $ar[0] . '/' . $displayArea . ' ' . $displayOffset,
+ 'displayArea' => $displayArea . ' ' . $displayOffset
);
}
return self::$timezones;
}
+ static function isValidTimeZone($tz) {
+ try {
+ new DateTimeZone($tz);
+ }
+ catch (Exception $e) {
+ return false;
+ }
+
+ return true;
+ }
+
+ static function toRegion($tz)
+ {
+ if(!self::isValidTimeZone($tz)) {
+ return '';
+ }
+
+ return explode('/', $tz)[0];
+ }
+
+ static function toArea($tz)
+ {
+ if(!self::isValidTimeZone($tz)) {
+ return '';
+ }
+
+ return explode('/', $tz)[1];
+ }
+ static function toTimeOffset($dt, $tz)
+ {
+ if(!self::isValidTimeZone($tz)) {
+ return '';
+ }
+
+ $date = new DateTime($dt, new DateTimeZone($tz));
+ return $date->format('P');
+ }
+
+ static function toDisplayArea($dt, $tz)
+ {
+ return str_replace('_', ' ', self::toArea($tz)) . ' (GMT ' . self::toTimeOffset($dt,$tz) . ')';
+
+ }
+
+ static function toDisplayName($dt, $tz)
+ {
+ return self::toRegion($tz) . '/' . self::toDisplayArea($dt, $tz);
+ }
}
\ No newline at end of file