|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BFW; |
|
4
|
|
|
|
|
5
|
|
|
use \DateTime; |
|
6
|
|
|
use \Exception; |
|
7
|
|
|
|
|
8
|
|
|
class Dates extends DateTime |
|
9
|
|
|
{ |
|
10
|
|
|
protected static $humainReadableI18n = [ |
|
11
|
|
|
'now' => 'Now', |
|
12
|
|
|
'since' => 'Since', |
|
13
|
|
|
'yesterday' => 'Testerday', |
|
14
|
|
|
'the' => 'The', |
|
15
|
|
|
'at' => 'at' |
|
16
|
|
|
]; |
|
17
|
|
|
|
|
18
|
|
|
protected static $humainReadableFormats = [ |
|
19
|
|
|
'dateSameYear' => 'm-d', |
|
20
|
|
|
'dateDifferentYear' => 'Y-m-d', |
|
21
|
|
|
'time' => 'H:i' |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
public static function getHumainReadableI18n() |
|
25
|
|
|
{ |
|
26
|
|
|
return self::$humainReadableI18n; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function setHumainReadableI18nKey($key, $value) |
|
30
|
|
|
{ |
|
31
|
|
|
self::$humainReadableI18n[$key] = $value; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function setHumainReadableI18n($value) |
|
35
|
|
|
{ |
|
36
|
|
|
self::$humainReadableI18n = $value; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public static function getHumainReadableFormats() |
|
40
|
|
|
{ |
|
41
|
|
|
return self::$humainReadableFormats; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function setHumainReadableFormatsKey($key, $value) |
|
45
|
|
|
{ |
|
46
|
|
|
self::$humainReadableFormats[$key] = $value; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public static function setHumainReadableFormats($value) |
|
50
|
|
|
{ |
|
51
|
|
|
self::$humainReadableFormats = $value; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Accesseur vers l'attribut $date |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getDate() |
|
58
|
|
|
{ |
|
59
|
|
|
return parent::format('Y-m-d H:i:sO'); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function getYear() |
|
63
|
|
|
{ |
|
64
|
|
|
return parent::format('Y'); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function getMonth() |
|
68
|
|
|
{ |
|
69
|
|
|
return parent::format('m'); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function getDay() |
|
73
|
|
|
{ |
|
74
|
|
|
return parent::format('d'); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getHour() |
|
78
|
|
|
{ |
|
79
|
|
|
return parent::format('H'); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getMinute() |
|
83
|
|
|
{ |
|
84
|
|
|
return parent::format('i'); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getSecond() |
|
88
|
|
|
{ |
|
89
|
|
|
return parent::format('s'); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
public function getZone() |
|
93
|
|
|
{ |
|
94
|
|
|
return parent::format('P'); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function modify($modify) |
|
98
|
|
|
{ |
|
99
|
|
|
$dateDepart = clone $this; |
|
100
|
|
|
parent::modify($modify); |
|
101
|
|
|
|
|
102
|
|
|
if ($dateDepart == $this) { |
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->modifyOthersKeywords($modify); |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function getModifyOthersKeywors() |
|
112
|
|
|
{ |
|
113
|
|
|
//Liste des possibilités qu'on permet |
|
114
|
|
|
$search = [ |
|
115
|
|
|
'an', 'ans', |
|
116
|
|
|
'mois', |
|
117
|
|
|
'jour', 'jours', |
|
118
|
|
|
'heure', 'heures', |
|
119
|
|
|
'minutes', |
|
120
|
|
|
'seconde', 'secondes' |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
//Liste des équivalent pour la fonction modify de DateTime |
|
124
|
|
|
$replace = [ |
|
125
|
|
|
'year', 'year', |
|
126
|
|
|
'month', |
|
127
|
|
|
'day', 'day', |
|
128
|
|
|
'hour', 'hour', |
|
129
|
|
|
'minute', |
|
130
|
|
|
'second', 'second' |
|
131
|
|
|
]; |
|
132
|
|
|
|
|
133
|
|
|
return (object) [ |
|
134
|
|
|
'search' => $search, |
|
135
|
|
|
'replace' => $replace |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
protected function modifyOthersKeywords($modify) |
|
140
|
|
|
{ |
|
141
|
|
|
$keywords = $this->getModifyOthersKeywors(); |
|
142
|
|
|
$match = []; |
|
143
|
|
|
|
|
144
|
|
|
//Regex sur le paramètre pour récupéré le type de modification |
|
145
|
|
|
if(preg_match('#(\+|\-)([0-9]+) ([a-z]+)#i', $modify, $match) !== 1) { |
|
146
|
|
|
throw new Exceptio('Date::modify pattern not match.'); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$keyword = str_replace( |
|
150
|
|
|
$keywords->search, |
|
151
|
|
|
$keywords->replace, |
|
152
|
|
|
strtolower($match[3]) |
|
153
|
|
|
); |
|
154
|
|
|
|
|
155
|
|
|
$dateDepart = clone $this; |
|
156
|
|
|
parent::modify($match[1].$match[2].' '.$keyword); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
if($dateDepart == $this) { |
|
159
|
|
|
throw new Exception('Parameter '.$match[3].' is unknown.'); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Renvoi au format pour SQL (postgresql) via un array |
|
165
|
|
|
* |
|
166
|
|
|
* @param bool $returnArray (default: false) Indique si on veux retourner |
|
167
|
|
|
* un string ayant tout, ou un array ayant la date et l'heure séparé |
|
168
|
|
|
* |
|
169
|
|
|
* @return string|array Le format pour SQL |
|
170
|
|
|
* Si string : aaaa-mm-jj hh:mm:ss |
|
171
|
|
|
* Si array : [0]=>partie date (aaaa-mm-jj), [1]=>partie heure (hh:mm:ss) |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getSql($returnArray = false) |
|
174
|
|
|
{ |
|
175
|
|
|
$date = $this->format('Y-m-d'); |
|
176
|
|
|
$heure = $this->format('H:i:s'); |
|
177
|
|
|
|
|
178
|
|
|
if ($returnArray) { |
|
179
|
|
|
return [$date, $heure]; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $date.' '.$heure; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Liste tous les timezone qui existe |
|
187
|
|
|
* |
|
188
|
|
|
* @return array La liste des timezone possible |
|
189
|
|
|
*/ |
|
190
|
|
|
public function lstTimeZone() |
|
191
|
|
|
{ |
|
192
|
|
|
return parent::getTimezone()->listIdentifiers(); |
|
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Liste les continents possible pour les timezones |
|
197
|
|
|
* |
|
198
|
|
|
* @return string[] La liste des continents |
|
199
|
|
|
*/ |
|
200
|
|
|
public function lstTimeZoneContinent() |
|
201
|
|
|
{ |
|
202
|
|
|
return [ |
|
203
|
|
|
'africa', |
|
204
|
|
|
'america', |
|
205
|
|
|
'antartica', |
|
206
|
|
|
'arctic', |
|
207
|
|
|
'asia', |
|
208
|
|
|
'atlantic', |
|
209
|
|
|
'australia', |
|
210
|
|
|
'europe', |
|
211
|
|
|
'indian', |
|
212
|
|
|
'pacific' |
|
213
|
|
|
]; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Liste des pays possible pour un continent donné |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $continent Le continent dans lequel on veux la liste des pays |
|
220
|
|
|
* |
|
221
|
|
|
* @return array La liste des pays pour le continent donné |
|
222
|
|
|
*/ |
|
223
|
|
|
public function lstTimeZonePays($continent) |
|
224
|
|
|
{ |
|
225
|
|
|
$lst_all = $this->lstTimeZone(); |
|
226
|
|
|
$return = []; |
|
227
|
|
|
|
|
228
|
|
|
$pos = false; |
|
|
|
|
|
|
229
|
|
|
foreach ($lst_all as $val) { |
|
230
|
|
|
$pos = strpos($val, $continent); |
|
231
|
|
|
|
|
232
|
|
|
if ($pos !== false) { |
|
233
|
|
|
$return[] = $val; |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
return $return; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
public function humainReadable($returnDateAndTime = true, $toLower = false) |
|
241
|
|
|
{ |
|
242
|
|
|
$actual = new Dates; |
|
243
|
|
|
$diff = parent::diff($actual); |
|
244
|
|
|
|
|
245
|
|
|
$returnTxt = (object) [ |
|
246
|
|
|
'date' => '', |
|
247
|
|
|
'time' => '' |
|
248
|
|
|
]; |
|
249
|
|
|
|
|
250
|
|
|
if ($actual == $this) { |
|
251
|
|
|
//A l'instant |
|
252
|
|
|
|
|
253
|
|
|
$returnTxt->date = self::$humainReadableI18n['now']; |
|
254
|
|
|
} elseif ($actual->format('d') !== parent::format('d')) { |
|
255
|
|
|
//Hier |
|
256
|
|
|
|
|
257
|
|
|
$returnTxt->date = self::$humainReadableI18n['yesterday']; |
|
258
|
|
|
$returnTxt->time = self::$humainReadableI18n['at'] |
|
259
|
|
|
.' ' |
|
260
|
|
|
.parent::format( |
|
261
|
|
|
self::$humainReadableFormats['time'] |
|
262
|
|
|
); |
|
263
|
|
|
} elseif ($diff->days === 0) { |
|
264
|
|
|
//Aujourd'hui |
|
265
|
|
|
|
|
266
|
|
|
$returnTxt->date = self::$humainReadableI18n['since']; |
|
267
|
|
|
|
|
268
|
|
|
if ($diff->h === 0 && $diff->m === 0) { |
|
269
|
|
|
$returnTxt->date .= $diff->s.'s'; |
|
270
|
|
|
} elseif ($diff->h === 0) { |
|
271
|
|
|
$returnTxt->date .= $diff->s.'min'; |
|
272
|
|
|
} else { |
|
273
|
|
|
$returnTxt->date .= $diff->h.'h'; |
|
274
|
|
|
} |
|
275
|
|
|
} else { |
|
276
|
|
|
$dateFormat = self::$humainReadableFormats['dateDifferentYear']; |
|
277
|
|
|
if ($actual->format('Y') === parent::format('Y')) { |
|
278
|
|
|
$dateFormat = self::$humainReadableFormats['dateSameYear']; |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
|
|
282
|
|
|
$returnTxt->date = self::$humainReadableI18n['the'] |
|
283
|
|
|
.' ' |
|
284
|
|
|
.parent::format($dateFormat); |
|
285
|
|
|
|
|
286
|
|
|
$returnTxt->time = self::$humainReadableI18n['at'] |
|
287
|
|
|
.' ' |
|
288
|
|
|
.parent::format( |
|
289
|
|
|
self::$humainReadableFormats['time'] |
|
290
|
|
|
); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
$txtReturn = $returnTxt->date; |
|
294
|
|
|
if ($returnDateAndTime === true && $returnTxt->time !== '') { |
|
295
|
|
|
$txtReturn .= ' '.$returnTxt->time; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
if ($toLower === true) { |
|
299
|
|
|
$txtReturn = mb_strtolower($txtReturn); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return $txtReturn; |
|
303
|
|
|
} |
|
304
|
|
|
} |
|
305
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.