1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Joy2362\PhpTimezone; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Exception; |
7
|
|
|
use DateTimeZone; |
8
|
|
|
use Illuminate\Support\Facades\Config; |
9
|
|
|
|
10
|
|
|
class TimeZoneService |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private array $regions = [ |
16
|
|
|
'Africa' => DateTimeZone::AFRICA, |
17
|
|
|
'America' => DateTimeZone::AMERICA, |
18
|
|
|
'Antarctica' => DateTimeZone::ANTARCTICA, |
19
|
|
|
'Asia' => DateTimeZone::ASIA, |
20
|
|
|
'Atlantic' => DateTimeZone::ATLANTIC, |
21
|
|
|
'Australia' => DateTimeZone::AUSTRALIA, |
22
|
|
|
'Europe' => DateTimeZone::EUROPE, |
23
|
|
|
'Indian' => DateTimeZone::INDIAN, |
24
|
|
|
'Pacific' => DateTimeZone::PACIFIC, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array|string[] |
29
|
|
|
*/ |
30
|
|
|
private array $supportedTimeZone = ['GMT', 'UTC']; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
public function getRegions(): array |
36
|
|
|
{ |
37
|
|
|
return array_keys($this->regions); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function getSupportedTimeZone(): array |
44
|
|
|
{ |
45
|
|
|
return $this->supportedTimeZone; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function list(): array |
52
|
|
|
{ |
53
|
|
|
$list = []; |
54
|
|
|
foreach ($this->regions as $region) { |
55
|
|
|
$list[] = $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? []); |
56
|
|
|
} |
57
|
|
|
return $list; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function listWithoutLabel(): array |
64
|
|
|
{ |
65
|
|
|
$list = []; |
66
|
|
|
foreach ($this->regions as $region) { |
67
|
|
|
$list[] = $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [] , false); |
68
|
|
|
} |
69
|
|
|
return $list; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
public function listWithoutValue(): array |
76
|
|
|
{ |
77
|
|
|
$list = []; |
78
|
|
|
foreach ($this->regions as $region) { |
79
|
|
|
$list[] = $this->getTimeZoneList(DateTimeZone::listIdentifiers($region) ?? [] , true , false); |
80
|
|
|
} |
81
|
|
|
return $list; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function listByRegion($region): array |
85
|
|
|
{ |
86
|
|
|
if (!array_key_exists($region, $this->regions)) { |
87
|
|
|
return []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->getTimeZoneList(DateTimeZone::listIdentifiers($this->regions[$region]) ?? []); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $label |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getValueFromLabel($label): string |
98
|
|
|
{ |
99
|
|
|
$str_zone = explode(') ', $label); |
100
|
|
|
return str_replace(' ', '_', $str_zone[1]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $value |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getLabelFromValue($value): string |
108
|
|
|
{ |
109
|
|
|
return $this->getLabel($value); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $timezone |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
private function getLabel($timezone): string |
117
|
|
|
{ |
118
|
|
|
try { |
119
|
|
|
$time = new DateTime('', new DateTimeZone($timezone)); |
120
|
|
|
$time_diff = $this->getTimeDiff($time); |
121
|
|
|
$zone = $this->getZone($time); |
122
|
|
|
|
123
|
|
|
$defaultTimeZone = Config::get('Timezone.DEFAULT_TIME_ZONE' , 'GMT'); |
124
|
|
|
$defaultTimeZone = in_array($defaultTimeZone, $this->supportedTimeZone) ? $defaultTimeZone : $this->supportedTimeZone[0]; |
125
|
|
|
return "({$defaultTimeZone} {$time_diff}) {$zone}"; |
126
|
|
|
} catch (Exception $ex) { |
127
|
|
|
return ''; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $time |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
private function getTimeDiff($time): string |
136
|
|
|
{ |
137
|
|
|
$time_diff_symbol = Config::get('Timezone.TIME_DIFF_SYMBOL','.'); |
138
|
|
|
$str_time_diff = $time->format('p'); |
139
|
|
|
return str_replace(':', $time_diff_symbol, $str_time_diff); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $time |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
private function getZone($time): string |
147
|
|
|
{ |
148
|
|
|
return str_replace('_', ' ', $time->format('e')); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param array $timezones |
153
|
|
|
* @param bool $isLabel |
154
|
|
|
* @param bool $isValue |
155
|
|
|
* @return array |
156
|
|
|
*/ |
157
|
|
|
private function getTimeZoneList(array $timezones , bool $isLabel = true , bool $isValue = true): array |
158
|
|
|
{ |
159
|
|
|
$label = Config::get('Timezone.LABEL_FIELD_NAME','label'); |
160
|
|
|
$value = Config::get('Timezone.VALUE_FIELD_NAME','value'); |
161
|
|
|
|
162
|
|
|
$data = []; |
163
|
|
|
|
164
|
|
|
foreach ($timezones as $timezone) { |
165
|
|
|
if($isLabel ){ |
166
|
|
|
$data[$label] = $this->getLabel($timezone); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if($isValue ){ |
170
|
|
|
$data[$value] = $timezone; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $data; |
176
|
|
|
} |
177
|
|
|
} |