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' => 'Yesterday', |
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); //Yeurk, but for personnal pattern, no choice |
|
|
|
|
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 Exception('Dates::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
|
|
|
//Yeurk, but I preferer send a Exception, not an error |
157
|
|
|
@parent::modify($match[1].$match[2].' '.$keyword); |
|
|
|
|
158
|
|
|
|
159
|
|
|
if ($dateDepart == $this) { |
160
|
|
|
throw new Exception( |
161
|
|
|
'Dates::modify Parameter '.$match[3].' is unknown.' |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Renvoi au format pour SQL (postgresql) via un array |
168
|
|
|
* |
169
|
|
|
* @param bool $returnArray (default: false) Indique si on veux retourner |
170
|
|
|
* un string ayant tout, ou un array ayant la date et l'heure séparé |
171
|
|
|
* |
172
|
|
|
* @return string|array Le format pour SQL |
173
|
|
|
* Si string : aaaa-mm-jj hh:mm:ss |
174
|
|
|
* Si array : [0]=>partie date (aaaa-mm-jj), [1]=>partie heure (hh:mm:ss) |
175
|
|
|
*/ |
176
|
|
|
public function getSqlFormat($returnArray = false) |
177
|
|
|
{ |
178
|
|
|
$date = $this->format('Y-m-d'); |
179
|
|
|
$heure = $this->format('H:i:s'); |
180
|
|
|
|
181
|
|
|
if ($returnArray) { |
182
|
|
|
return [$date, $heure]; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $date.' '.$heure; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Liste tous les timezone qui existe |
190
|
|
|
* |
191
|
|
|
* @return array La liste des timezone possible |
192
|
|
|
*/ |
193
|
|
|
public function lstTimeZone() |
194
|
|
|
{ |
195
|
|
|
return parent::getTimezone()->listIdentifiers(); |
|
|
|
|
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Liste les continents possible pour les timezones |
200
|
|
|
* |
201
|
|
|
* @return string[] La liste des continents |
202
|
|
|
*/ |
203
|
|
|
public function lstTimeZoneContinent() |
204
|
|
|
{ |
205
|
|
|
return [ |
206
|
|
|
'africa', |
207
|
|
|
'america', |
208
|
|
|
'antartica', |
209
|
|
|
'arctic', |
210
|
|
|
'asia', |
211
|
|
|
'atlantic', |
212
|
|
|
'australia', |
213
|
|
|
'europe', |
214
|
|
|
'indian', |
215
|
|
|
'pacific' |
216
|
|
|
]; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Liste des pays possible pour un continent donné |
221
|
|
|
* |
222
|
|
|
* @param string $continent Le continent dans lequel on veux la liste des pays |
223
|
|
|
* |
224
|
|
|
* @return array La liste des pays pour le continent donné |
225
|
|
|
*/ |
226
|
|
|
public function lstTimeZonePays($continent) |
227
|
|
|
{ |
228
|
|
|
$lst_all = $this->lstTimeZone(); |
229
|
|
|
$return = []; |
230
|
|
|
|
231
|
|
|
$pos = false; |
|
|
|
|
232
|
|
|
foreach ($lst_all as $val) { |
233
|
|
|
$pos = strpos($val, $continent); |
234
|
|
|
|
235
|
|
|
if ($pos !== false) { |
236
|
|
|
$return[] = $val; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $return; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function humainReadable($returnDateAndTime = true, $toLower = false) |
244
|
|
|
{ |
245
|
|
|
$actual = new Dates; |
246
|
|
|
$diff = parent::diff($actual); |
247
|
|
|
|
248
|
|
|
$returnTxt = (object) [ |
249
|
|
|
'date' => '', |
250
|
|
|
'time' => '' |
251
|
|
|
]; |
252
|
|
|
|
253
|
|
|
if ($actual == $this) { |
254
|
|
|
//A l'instant |
255
|
|
|
|
256
|
|
|
$returnTxt->date = self::$humainReadableI18n['now']; |
257
|
|
|
} elseif ($diff->d === 1 && $diff->m === 0 && $diff->y === 0) { |
258
|
|
|
//Hier |
259
|
|
|
|
260
|
|
|
$returnTxt->date = self::$humainReadableI18n['yesterday']; |
261
|
|
|
$returnTxt->time = self::$humainReadableI18n['at'] |
262
|
|
|
.' ' |
263
|
|
|
.parent::format( |
264
|
|
|
self::$humainReadableFormats['time'] |
265
|
|
|
); |
266
|
|
|
} elseif ($diff->days === 0) { |
267
|
|
|
//Aujourd'hui |
268
|
|
|
|
269
|
|
|
$returnTxt->date = self::$humainReadableI18n['since'].' '; |
270
|
|
|
|
271
|
|
|
if ($diff->h === 0 && $diff->i === 0) { |
272
|
|
|
$returnTxt->date .= $diff->s.'s'; |
273
|
|
|
} elseif ($diff->h === 0) { |
274
|
|
|
$returnTxt->date .= $diff->i.'min'; |
275
|
|
|
} else { |
276
|
|
|
$returnTxt->date .= $diff->h.'h'; |
277
|
|
|
} |
278
|
|
|
} else { |
279
|
|
|
$dateFormat = self::$humainReadableFormats['dateDifferentYear']; |
280
|
|
|
if ($actual->format('Y') === parent::format('Y')) { |
281
|
|
|
$dateFormat = self::$humainReadableFormats['dateSameYear']; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
$returnTxt->date = self::$humainReadableI18n['the'] |
285
|
|
|
.' ' |
286
|
|
|
.parent::format($dateFormat); |
287
|
|
|
|
288
|
|
|
$returnTxt->time = self::$humainReadableI18n['at'] |
289
|
|
|
.' ' |
290
|
|
|
.parent::format( |
291
|
|
|
self::$humainReadableFormats['time'] |
292
|
|
|
); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$txtReturn = $returnTxt->date; |
296
|
|
|
if ($returnDateAndTime === true && $returnTxt->time !== '') { |
297
|
|
|
$txtReturn .= ' '.$returnTxt->time; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
if ($toLower === true) { |
301
|
|
|
$txtReturn = mb_strtolower($txtReturn); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
return $txtReturn; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
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 theSon
calls the wrong method in the parent class.