-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimezoneWidget.php
155 lines (122 loc) · 4.58 KB
/
TimezoneWidget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/*
TimezoneWidget.php - LGPL license
https://github.com/peterjtracey/timezoneWidget
*/
class TimezoneWidget {
const TZ_REGIONS = array(
\DateTimeZone::AFRICA => "Africa",
\DateTimeZone::AMERICA => "America",
\DateTimeZone::ANTARCTICA => "Antarctica",
\DateTimeZone::ASIA => "Asia",
\DateTimeZone::ATLANTIC => "Atlantic",
\DateTimeZone::AUSTRALIA => "Australia",
\DateTimeZone::EUROPE => "Europe",
\DateTimeZone::INDIAN => "Indian",
\DateTimeZone::PACIFIC => "Pacific",
);
const TZ_REGION_SYSTEM_US = 1024;
const TZ_REGION_DEFAULT = \DateTimeZone::AMERICA;
const TIMEZONE_DEFAULT = 'America/New_York';
public static function timezoneValues() {
$us_timezones = [
'America/New_York',
'America/Chicago',
'America/Denver',
'America/Los_Angeles',
'America/Anchorage',
'Pacific/Honolulu'
];
$objUS = new \stdClass();
$objUS->id = self::TZ_REGION_SYSTEM_US;
$objUS->label = "United States";
$objUS->timezones = [];
foreach ($us_timezones as $timezone) {
$tz = new \DateTimeZone($timezone);
$dateTime = new \DateTime();
$dateTime->setTimeZone($tz);
$offset = $tz->getOffset($dateTime);
$tzObj = new \stdClass();
$tzObj->labels = [$timezone];
$tzObj->offsetNumber = $offset;
$offset_prefix = $offset < 0 ? '-' : '+';
$offset_formatted = gmdate( 'H:i', abs($offset) );
$pretty_offset = $dateTime->format('T') . " (UTC${offset_prefix}${offset_formatted})";
$tzObj->offsetLabel = $pretty_offset;
$objUS->timezones[] = $tzObj;
}
$timezones = [
$objUS
];
foreach( self::TZ_REGIONS as $region => $label )
{
$region_timezones = [];
$region_info = \DateTimeZone::listIdentifiers( $region );
$labels = \DateTimeZone::listIdentifiers( $region );
foreach ($labels as $label) {
$tz = new \DateTimeZone($label);
$offset = $tz->getOffset(new \DateTime);
if (array_key_exists($offset, $region_timezones)) {
$region_timezones[$offset]->labels[] = $label;
} else {
$tzObj = new \stdClass();
$tzObj->labels = [$label];
$tzObj->offsetNumber = $offset;
$offset_prefix = $offset < 0 ? '-' : '+';
$offset_formatted = gmdate( 'H:i', abs($offset) );
$pretty_offset = "UTC${offset_prefix}${offset_formatted}";
$tzObj->offsetLabel = $pretty_offset;
$region_timezones[$offset] = $tzObj;
}
}
// sort timezone by offset
ksort($region_timezones);
$regionObj = new \stdClass();
$regionObj->id = $region;
$regionObj->label = self::TZ_REGIONS[$region];
$regionObj->timezones = array_values($region_timezones);
$timezones[] = $regionObj;
}
return $timezones;
}
public static function validateTimezone($name, $regionid = null) {
$tzData = self::timezoneValues();
$regionValid = false;
if ($regionid == null) {
$regionValid = true;
}
$nameValid = false;
$usa = true;
foreach ($tzData as $region) {
foreach ($region->timezones as $timezone) {
foreach ($timezone->labels as $label) {
if ($name == $label) {
if (!$regionValid) {
if ($regionid == 1024 && $usa) {
return true;
} else if (self::TZ_REGIONS[$region->id] != $regionid) {
return false;
}
}
return true;
}
}
}
$usa = false;
}
return false;
}
public static function regionForTimezone($name) {
$tzData = self::timezoneValues();
foreach ($tzData as $region) {
foreach ($region->timezones as $timezone) {
foreach ($timezone->labels as $label) {
if ($name == $label) {
return $region->id;
}
}
}
}
return false;
}
}