1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ************************** |
4
|
|
|
* COSTANTS |
5
|
|
|
* ************************** |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* SECONDS SHART CUTS |
10
|
|
|
*/ |
11
|
|
|
const SECOND_IN_SECOND = 1; |
12
|
|
|
const MINUTE_IN_SECOND = 60; |
13
|
|
|
const HOUR_IN_SECOND = 3600; |
14
|
|
|
const DAY_IN_SECOND = 86400; |
15
|
|
|
const WEEK_IN_SECOND = 604800; |
16
|
|
|
const MONTH_IN_SECOND = 2592000; |
17
|
|
|
const YEAR_IN_SECOND = 31557600; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* DATE TIME FORMAT |
21
|
|
|
*/ |
22
|
|
|
const DATE_TIME_FORMAT_ISO = 'Y-m-d H:i:s'; |
23
|
|
|
const DATE_TIME_FORMAT_ITA = 'd/m/Y H:i:s'; |
24
|
|
|
const DATE_FORMAT_ISO = 'Y-m-d'; |
25
|
|
|
const DATE_FORMAT_ITA = 'd/m/Y'; |
26
|
|
|
const TIME_FORMAT_ISO = 'H:i:s'; |
27
|
|
|
const TIME_FORMAT_ITA = 'H:i:s'; |
28
|
|
|
/** |
29
|
|
|
* The day constants. |
30
|
|
|
*/ |
31
|
|
|
const SUNDAY = 0; |
32
|
|
|
const MONDAY = 1; |
33
|
|
|
const TUESDAY = 2; |
34
|
|
|
const WEDNESDAY = 3; |
35
|
|
|
const THURSDAY = 4; |
36
|
|
|
const FRIDAY = 5; |
37
|
|
|
const SATURDAY = 6; |
38
|
|
|
|
39
|
|
|
const DAYS_ITA_ARR = [ |
40
|
|
|
0 => 'Domenica', |
41
|
|
|
1 => 'lunedi', |
42
|
|
|
2 => 'martedi', |
43
|
|
|
3 => 'mercoledi', |
44
|
|
|
4 => 'giovedi', |
45
|
|
|
5 => 'venerdi', |
46
|
|
|
6 => 'sabato', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
const DAYS_ENG_ARR = [ |
50
|
|
|
0 => 'Sunday', |
51
|
|
|
1 => 'Monday', |
52
|
|
|
2 => 'Tuesday', |
53
|
|
|
3 => 'Wednesday', |
54
|
|
|
4 => 'Thursday', |
55
|
|
|
5 => 'Friday', |
56
|
|
|
6 => 'Saturday', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* The month constants. |
61
|
|
|
*/ |
62
|
|
|
const GENNAIO = 0; |
63
|
|
|
const FEBBRAIO = 1; |
64
|
|
|
const MARZO = 2; |
65
|
|
|
const APRILE = 3; |
66
|
|
|
const MAGGIO = 4; |
67
|
|
|
const GIUGNO = 5; |
68
|
|
|
const LUGLIO = 6; |
69
|
|
|
const AGOSTO = 7; |
70
|
|
|
const SETTEMBRE = 8; |
71
|
|
|
const OTTOBRE = 9; |
72
|
|
|
const NOVEMBRE = 10; |
73
|
|
|
const DICEMBRE = 11; |
74
|
|
|
|
75
|
|
|
const MONTHS_ITA_ARR = [ |
76
|
|
|
0 => 'Gennaio', |
77
|
|
|
1 => 'Febbraio', |
78
|
|
|
2 => 'Marzo', |
79
|
|
|
3 => 'Aprile', |
80
|
|
|
4 => 'Maggio', |
81
|
|
|
5 => 'Giugno', |
82
|
|
|
6 => 'Luglio', |
83
|
|
|
7 => 'Agosto', |
84
|
|
|
8 => 'Settembre', |
85
|
|
|
9 => 'Ottobre', |
86
|
|
|
10 => 'Novembre', |
87
|
|
|
11 => 'Dicembre', |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
const MONTHS_ITA_ARR_1_BASED = [ |
91
|
|
|
1 => 'Gennaio', |
92
|
|
|
2 => 'Febbraio', |
93
|
|
|
3 => 'Marzo', |
94
|
|
|
4 => 'Aprile', |
95
|
|
|
5 => 'Maggio', |
96
|
|
|
6 => 'Giugno', |
97
|
|
|
7 => 'Luglio', |
98
|
|
|
8 => 'Agosto', |
99
|
|
|
9 => 'Settembre', |
100
|
|
|
10 => 'Ottobre', |
101
|
|
|
11 => 'Novembre', |
102
|
|
|
12 => 'Dicembre', |
103
|
|
|
]; |
104
|
|
|
|
105
|
|
|
const MONTHS_SHORT_ITA_ARR = [ |
106
|
|
|
0 => 'Gen', |
107
|
|
|
1 => 'Feb', |
108
|
|
|
2 => 'Mar', |
109
|
|
|
3 => 'Apr', |
110
|
|
|
4 => 'Mag', |
111
|
|
|
5 => 'Giu', |
112
|
|
|
6 => 'Lug', |
113
|
|
|
7 => 'Ago', |
114
|
|
|
8 => 'Set', |
115
|
|
|
9 => 'Ott', |
116
|
|
|
10 => 'Nov', |
117
|
|
|
11 => 'Dic', |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
const MONTHS_SHORT_ITA_ARR_1_BASED = [ |
121
|
|
|
1 => 'Gen', |
122
|
|
|
2 => 'Feb', |
123
|
|
|
3 => 'Mar', |
124
|
|
|
4 => 'Apr', |
125
|
|
|
5 => 'Mag', |
126
|
|
|
6 => 'Giu', |
127
|
|
|
7 => 'Lug', |
128
|
|
|
8 => 'Ago', |
129
|
|
|
9 => 'Set', |
130
|
|
|
10 => 'Ott', |
131
|
|
|
11 => 'Nov', |
132
|
|
|
12 => 'Dic', |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* ************************** |
137
|
|
|
* HELPERS |
138
|
|
|
* ************************** |
139
|
|
|
*/ |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get Carbon istance by string $date in 'Y-m-d H:i:s' format. |
143
|
|
|
* @param string $date |
144
|
|
|
* @return \Carbon\Carbon |
145
|
|
|
*/ |
146
|
|
|
function carbonFromIsoDateTime(string $date): Carbon\Carbon |
147
|
|
|
{ |
148
|
|
|
return Carbon\Carbon::createFromFormat(DATE_TIME_FORMAT_ISO, $date); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get Carbon istance by string $date in 'Y-m-d' format. |
153
|
|
|
* @param string $date |
154
|
|
|
* @return \Carbon\Carbon |
155
|
|
|
*/ |
156
|
|
|
function carbonFromIsoDate(string $date): Carbon\Carbon |
157
|
|
|
{ |
158
|
|
|
return Carbon\Carbon::createFromFormat(DATE_FORMAT_ISO, $date); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get Carbon istance by string $date in 'd/m/Y H:i:s' format. |
163
|
|
|
* @param string $date |
164
|
|
|
* @return \Carbon\Carbon |
165
|
|
|
*/ |
166
|
|
|
function carbonFromItaDateTime(string $date): Carbon\Carbon |
167
|
|
|
{ |
168
|
|
|
return Carbon\Carbon::createFromFormat(DATE_TIME_FORMAT_ITA, $date); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get Carbon istance by string $date in 'd/m/Y' format. |
173
|
|
|
* @param string $date |
174
|
|
|
* @return \Carbon\Carbon |
175
|
|
|
*/ |
176
|
|
|
function carbonFromItaDate(string $date): Carbon\Carbon |
177
|
|
|
{ |
178
|
|
|
return Carbon\Carbon::createFromFormat(DATE_FORMAT_ITA, $date); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get Carbon istance by string $date in $format format. |
183
|
|
|
* If $date string doesn't match passed forma, return Carbon::create(). |
184
|
|
|
* @param string $date |
185
|
|
|
* @param string $format |
186
|
|
|
* @return \Carbon\Carbon |
187
|
|
|
*/ |
188
|
|
|
function carbon(string $date, string $format = DATE_TIME_FORMAT_ISO): Carbon\Carbon |
189
|
|
|
{ |
190
|
|
|
return Carbon\Carbon::createFromFormat($format, $date); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* int to roman number |
195
|
|
|
* @param int|null $year |
196
|
|
|
* @return string |
197
|
|
|
* @see https://github.com/spatie-custom/blender/blob/master/app/Foundation/helpers.php |
198
|
|
|
*/ |
199
|
|
|
function roman_year(int $year = null): string |
200
|
|
|
{ |
201
|
|
|
if (!is_numeric($year)) { |
202
|
|
|
$year = (int)date('Y'); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$result = ''; |
206
|
|
|
|
207
|
|
|
$romanNumerals = [ |
208
|
|
|
'M' => 1000, |
209
|
|
|
'CM' => 900, |
210
|
|
|
'D' => 500, |
211
|
|
|
'CD' => 400, |
212
|
|
|
'C' => 100, |
213
|
|
|
'XC' => 90, |
214
|
|
|
'L' => 50, |
215
|
|
|
'XL' => 40, |
216
|
|
|
'X' => 10, |
217
|
|
|
'IX' => 9, |
218
|
|
|
'V' => 5, |
219
|
|
|
'IV' => 4, |
220
|
|
|
'I' => 1, |
221
|
|
|
]; |
222
|
|
|
|
223
|
|
|
foreach ($romanNumerals as $roman => $yearNumber) { |
224
|
|
|
// Divide to get matches |
225
|
|
|
$matches = (int)($year / $yearNumber); |
226
|
|
|
|
227
|
|
|
// Assign the roman char * $matches |
228
|
|
|
$result .= str_repeat($roman, $matches); |
229
|
|
|
|
230
|
|
|
// Substract from the number |
231
|
|
|
$year = $year % $yearNumber; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $result; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Split date iso |
239
|
|
|
* If $date is not valid set to '0000-00-00'. |
240
|
|
|
* If $segment is not valid set to 'y'. |
241
|
|
|
* @param string $date |
242
|
|
|
* @param string $segment allowed value: d, giorno, m, mese, Y, anno |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
function partialsDateIso(string $date, string $segment = 'Y') : string |
246
|
|
|
{ |
247
|
|
|
if (isNullOrEmpty($segment)) { |
248
|
|
|
$segment = 'Y'; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
if (isNullOrEmpty($date) || (!isDateIso($date) && !isDateTimeIso($date))) { |
252
|
|
|
$date = '0000-00-00'; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
switch ($segment) { |
256
|
|
|
case 'giorno || d': |
257
|
|
|
$result = substr($date, 8, 2); |
258
|
|
|
break; |
259
|
|
|
case 'mese || m': |
260
|
|
|
$result = substr($date, 5, 2); |
261
|
|
|
break; |
262
|
|
|
case 'anno || Y': |
263
|
|
|
default: |
264
|
|
|
$result = substr($date, 0, 4); |
265
|
|
|
break; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return $result; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Funzione per trasformare la data da Iso a italiano |
273
|
|
|
* If date is invalid return '00/00/0000'. |
274
|
|
|
* @param string $date |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
View Code Duplication |
function dateIsoToIta(string $date = "") : string |
|
|
|
|
278
|
|
|
{ |
279
|
|
|
if (isNullOrEmpty($date) || !isDateIso($date)) { |
280
|
|
|
return '00/00/0000'; |
281
|
|
|
} |
282
|
|
|
$arr_data = preg_split('[-]', $date); |
283
|
|
|
return $arr_data[2] . "/" . $arr_data[1] . "/" . $arr_data[0]; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* funzione per trasformare la data da italiano a Iso |
288
|
|
|
* If date is invalid return '0000-00-00'. |
289
|
|
|
* @param string $date |
290
|
|
|
* @return string |
291
|
|
|
*/ |
292
|
|
View Code Duplication |
function dateItaToIso(string $date = "") : string |
|
|
|
|
293
|
|
|
{ |
294
|
|
|
if (isNullOrEmpty($date) || !isDateIso($date)) { |
295
|
|
|
return '0000-00-00'; |
296
|
|
|
} |
297
|
|
|
$arr_data = preg_split('/[\/.-]/', $date); |
298
|
|
|
return $arr_data[2] . '-' . $arr_data[1] . '-' . $arr_data[0]; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Return month name by number. |
303
|
|
|
* If $monthNumber if out of range, return empty string. |
304
|
|
|
* @param $monthNumber |
305
|
|
|
* @param bool $nameComplete if se to false (default) return the short form (Gen, Feb,...). |
306
|
|
|
* @return string |
307
|
|
|
*/ |
308
|
|
|
function monthFromNumber(int $monthNumber, bool $nameComplete = false) : string |
309
|
|
|
{ |
310
|
|
|
if ($monthNumber < 1 || $monthNumber > 12) { |
311
|
|
|
return ''; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
return $nameComplete ? MONTHS_ITA_ARR_1_BASED[$monthNumber] : MONTHS_SHORT_ITA_ARR_1_BASED[$monthNumber]; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Funzione per trasformare la data da Iso a italiano specifata |
319
|
|
|
* @param string $data |
320
|
|
|
* @param bool $ShowDayName |
321
|
|
|
* @param bool $ShortDayName |
322
|
|
|
* @return string |
323
|
|
|
*/ |
324
|
|
|
function dateIsoToItaSpec(string $data = "", bool $ShowDayName = false, bool $ShortDayName = false) : string |
325
|
|
|
{ |
326
|
|
|
if (isNullOrEmpty($data) || !isDateIso($data)) { |
327
|
|
|
return "00/00/0000"; |
328
|
|
|
} else { |
329
|
|
|
$arr_data = explode('-', $data); |
330
|
|
|
|
331
|
|
|
if (substr($arr_data[2], 0, 1) == 0) { |
332
|
|
|
$arr_data[2] = substr($arr_data[2], 1, 1); |
333
|
|
|
} |
334
|
|
|
if (!$ShowDayName) { |
335
|
|
|
return $arr_data[2] . " " . MONTHS_ITA_ARR[$arr_data[1] - 1] . " " . $arr_data[0]; |
336
|
|
|
} else { |
337
|
|
|
$dayName = DAYS_ITA_ARR[(int)date("w", mktime(0, 0, 0, $arr_data[1], $arr_data[2], $arr_data[0]))]; |
338
|
|
|
return ($ShortDayName ? substr($dayName, 0, |
339
|
|
|
3) : $dayName) . ' ' . $arr_data[2] . ' ' . MONTHS_ITA_ARR[$arr_data[1] - 1] . ' ' . $arr_data[0]; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* @param string $data |
346
|
|
|
* @return string |
347
|
|
|
*/ |
348
|
|
|
function getNameDayFromDateIso(string $data) : string |
349
|
|
|
{ |
350
|
|
|
if (!isDateIso($data)) { |
351
|
|
|
return ''; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$dateItaSpec = dateIsoToItaSpec($data, true); |
355
|
|
|
$arrDate = explode(' ', trim($dateItaSpec)); |
356
|
|
|
$giorno = $arrDate[0]; |
357
|
|
|
|
358
|
|
|
return $giorno; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* @param string $dataTimeIso |
363
|
|
|
* @return string |
364
|
|
|
*/ |
365
|
|
|
function getTimeFromDateTimeIso(string $dataTimeIso) : string |
366
|
|
|
{ |
367
|
|
|
if (!isDateTimeIso($dataTimeIso)) { |
368
|
|
|
return '00:00'; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
return substr($dataTimeIso, 10, 6); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Determine difference in year between two date. |
376
|
|
|
* |
377
|
|
|
* @param string $data1 date ('Y-m-d') or datetime ('Y-m-d H:i:s') |
378
|
|
|
* @param string $data2 date ('Y-m-d') or datetime ('Y-m-d H:i:s') |
379
|
|
|
* |
380
|
|
|
* @return int |
381
|
|
|
*/ |
382
|
|
|
function diff_in_year($data1, $data2 = '') : int |
383
|
|
|
{ |
384
|
|
|
if (isNullOrEmpty($data1)) { |
385
|
|
|
return 0; |
386
|
|
|
} |
387
|
|
|
if (isNullOrEmpty($data2)) { |
388
|
|
|
$data2 = date('Y-m-d'); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
$cdate1 = new DateTime(date('Y-m-d', strtotime($data1))); |
392
|
|
|
$interval = $cdate1->diff($data2); |
393
|
|
|
return $interval->y; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Determine the age by date Of Birthday. |
398
|
|
|
* |
399
|
|
|
* @param string $dateOfBirthday date ('Y-m-d') or datetime ('Y-m-d H:i:s') Date Of Birthday |
400
|
|
|
* |
401
|
|
|
* @return int |
402
|
|
|
*/ |
403
|
|
|
function age($dateOfBirthday) : int |
404
|
|
|
{ |
405
|
|
|
return date_diff(date('Y-m-d'), $dateOfBirthday); |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Returns AM or PM, based on a given hour (in 24 hour format). |
410
|
|
|
* |
411
|
|
|
* $type = Date::ampm(12); // PM |
412
|
|
|
* $type = Date::ampm(1); // AM |
413
|
|
|
* |
414
|
|
|
* @param integer $hour number of the hour |
415
|
|
|
* @return string |
416
|
|
|
* @see https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php |
417
|
|
|
*/ |
418
|
|
|
function ampm($hour) |
419
|
|
|
{ |
420
|
|
|
// Always integer |
421
|
|
|
$hour = (int) $hour; |
422
|
|
|
return ($hour > 11) ? 'PM' : 'AM'; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
/** |
426
|
|
|
* Adjusts a non-24-hour number into a 24-hour number. |
427
|
|
|
* |
428
|
|
|
* $hour = Date::adjust(3, 'pm'); // 15 |
429
|
|
|
* |
430
|
|
|
* @param integer $hour hour to adjust |
431
|
|
|
* @param string $ampm AM or PM |
432
|
|
|
* @return string |
433
|
|
|
* @see https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php |
434
|
|
|
*/ |
435
|
|
|
function ampm2Number($hour, $ampm) |
436
|
|
|
{ |
437
|
|
|
$hour = (int) $hour; |
438
|
|
|
$ampm = strtolower($ampm); |
439
|
|
|
switch ($ampm) { |
440
|
|
|
case 'am': |
441
|
|
|
if ($hour == 12) { |
442
|
|
|
$hour = 0; |
443
|
|
|
} |
444
|
|
|
break; |
445
|
|
|
case 'pm': |
446
|
|
|
if ($hour < 12) { |
447
|
|
|
$hour += 12; |
448
|
|
|
} |
449
|
|
|
break; |
450
|
|
|
} |
451
|
|
|
return sprintf('%02d', $hour); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* Returns the difference between a time and now in a "fuzzy" way. |
456
|
|
|
* Displaying a fuzzy time instead of a date is usually faster to read and understand. |
457
|
|
|
* |
458
|
|
|
* $span = Date::fuzzy_span(time() - 10); // "moments ago" |
459
|
|
|
* $span = Date::fuzzy_span(time() + 20); // "in moments" |
460
|
|
|
* |
461
|
|
|
* A second parameter is available to manually set the "local" timestamp, |
462
|
|
|
* however this parameter shouldn't be needed in normal usage and is only |
463
|
|
|
* included for unit tests |
464
|
|
|
* |
465
|
|
|
* @param integer $timestamp "remote" timestamp |
466
|
|
|
* @param integer $local_timestamp "local" timestamp, defaults to time() |
467
|
|
|
* @return string $locale default 'IT' otherwise 'EN' |
468
|
|
|
* @return string |
469
|
|
|
* @see https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php |
470
|
|
|
*/ |
471
|
|
|
function fuzzySpan($timestamp, $local_timestamp = null, $locale='IT') |
472
|
|
|
{ |
473
|
|
|
$local_timestamp = ($local_timestamp === null) ? time() : (int) $local_timestamp; |
474
|
|
|
// Determine the difference in seconds |
475
|
|
|
$offset = abs($local_timestamp - $timestamp); |
476
|
|
|
if ($offset <= MINUTE_IN_SECOND) { |
477
|
|
|
$span = $locale=='EN' ? 'moments' : 'attimi'; |
478
|
|
|
} elseif ($offset < (MINUTE_IN_SECOND * 20)) { |
479
|
|
|
$span = $locale=='EN' ? 'a few minutes' : 'qualche minuto'; |
480
|
|
|
} elseif ($offset < HOUR_IN_SECOND) { |
481
|
|
|
$span = $locale=='EN' ? 'less than an hour' : 'meno di un ora'; |
482
|
|
|
} elseif ($offset < (HOUR_IN_SECOND * 4)) { |
483
|
|
|
$span = $locale=='EN' ? 'a couple of hours' : 'un paio di ore'; |
484
|
|
|
} elseif ($offset < DAY_IN_SECOND) { |
485
|
|
|
$span = $locale=='EN' ? 'less than a day' : 'meno di un giorno'; |
486
|
|
|
} elseif ($offset < (DAY_IN_SECOND * 2)) { |
487
|
|
|
$span = $locale=='EN' ? 'about a day' : 'circa un giorno'; |
488
|
|
|
} elseif ($offset < (DAY_IN_SECOND * 4)) { |
489
|
|
|
$span = $locale=='EN' ? 'a couple of days' : 'un paio di giorni'; |
490
|
|
|
} elseif ($offset < WEEK_IN_SECOND) { |
491
|
|
|
$span = $locale=='EN' ? 'less than a week' : 'meno di una settimana'; |
492
|
|
|
} elseif ($offset < (WEEK_IN_SECOND * 2)) { |
493
|
|
|
$span = $locale=='EN' ? 'about a week' : 'circa una settimana'; |
494
|
|
|
} elseif ($offset < MONTH_IN_SECOND) { |
495
|
|
|
$span = $locale=='EN' ? 'less than a month' : 'meno di un mese'; |
496
|
|
|
} elseif ($offset < (MONTH_IN_SECOND * 2)) { |
497
|
|
|
$span = $locale=='EN' ? 'about a month' : 'circa un mese'; |
498
|
|
|
} elseif ($offset < (MONTH_IN_SECOND * 4)) { |
499
|
|
|
$span = $locale=='EN' ? 'a couple of months' : 'un paio di mesi'; |
500
|
|
|
} elseif ($offset < YEAR_IN_SECOND) { |
501
|
|
|
$span = $locale=='EN' ? 'less than a year' : 'meno di un anno'; |
502
|
|
|
} elseif ($offset < (YEAR_IN_SECOND * 2)) { |
503
|
|
|
$span = $locale=='EN' ? 'about a year' : 'circa un anno'; |
504
|
|
|
} elseif ($offset < (YEAR_IN_SECOND * 4)) { |
505
|
|
|
$span = $locale=='EN' ? 'a couple of years' : 'un paio di anni'; |
506
|
|
|
} elseif ($offset < (YEAR_IN_SECOND * 8)) { |
507
|
|
|
$span = $locale=='EN' ? 'a few years' : 'qualche anno'; |
508
|
|
|
} elseif ($offset < (YEAR_IN_SECOND * 12)) { |
509
|
|
|
$span = $locale=='EN' ? 'about a decade' : 'circa un decennio'; |
510
|
|
|
} elseif ($offset < (YEAR_IN_SECOND * 24)) { |
511
|
|
|
$span = $locale=='EN' ? 'a couple of decades' : 'una coppia di decenni'; |
512
|
|
|
} elseif ($offset < (YEAR_IN_SECOND * 64)) { |
513
|
|
|
$span = $locale=='EN' ? 'several decades' : 'diversi decenni'; |
514
|
|
|
} else { |
515
|
|
|
$span = $locale=='EN' ? 'a long time' : 'un lungo periodo'; |
516
|
|
|
} |
517
|
|
|
if ($timestamp <= $local_timestamp) { |
518
|
|
|
// This is in the past |
519
|
|
|
return $span . ($locale=='EN' ? ' ago' : ' fà'); |
520
|
|
|
} else { |
521
|
|
|
// This in the future |
522
|
|
|
return ($locale=='EN' ? 'in ' : 'fra ') . $span; |
523
|
|
|
} |
524
|
|
|
} |
525
|
|
|
/** |
526
|
|
|
* Converts a UNIX timestamp to DOS format. There are very few cases where |
527
|
|
|
* this is needed, but some binary formats use it (eg: zip files.) |
528
|
|
|
* Converting the other direction is done using {@link Date::dos2unix}. |
529
|
|
|
* |
530
|
|
|
* $dos = Date::unix2dos($unix); |
531
|
|
|
* |
532
|
|
|
* @param integer $timestamp UNIX timestamp |
533
|
|
|
* @return integer |
534
|
|
|
* @see https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php |
535
|
|
|
*/ |
536
|
|
|
function unixTimestamp2dos($timestamp = null) |
537
|
|
|
{ |
538
|
|
|
$timestamp = getdate($timestamp); |
539
|
|
|
if ($timestamp['year'] < 1980) { |
540
|
|
|
return (1 << 21 | 1 << 16); |
541
|
|
|
} |
542
|
|
|
$timestamp['year'] -= 1980; |
543
|
|
|
// What voodoo is this? I have no idea... Geert can explain it though, |
544
|
|
|
// and that's good enough for me. |
545
|
|
|
return ($timestamp['year'] << 25 | $timestamp['mon'] << 21 | |
546
|
|
|
$timestamp['mday'] << 16 | $timestamp['hours'] << 11 | |
547
|
|
|
$timestamp['minutes'] << 5 | $timestamp['seconds'] >> 1); |
548
|
|
|
} |
549
|
|
|
/** |
550
|
|
|
* Converts a DOS timestamp to UNIX format.There are very few cases where |
551
|
|
|
* this is needed, but some binary formats use it (eg: zip files.) |
552
|
|
|
* Converting the other direction is done using {@link Date::unix2dos}. |
553
|
|
|
* |
554
|
|
|
* $unix = Date::dos2unix($dos); |
555
|
|
|
* |
556
|
|
|
* @param integer|bool $timestamp DOS timestamp |
557
|
|
|
* @return integer |
558
|
|
|
* @see https://github.com/kohana/ohanzee-helpers/blob/master/src/Date.php |
559
|
|
|
*/ |
560
|
|
|
function dos2unixTimestamp($timestamp = false) |
561
|
|
|
{ |
562
|
|
|
$sec = 2 * ($timestamp & 0x1f); |
563
|
|
|
$min = ($timestamp >> 5) & 0x3f; |
564
|
|
|
$hrs = ($timestamp >> 11) & 0x1f; |
565
|
|
|
$day = ($timestamp >> 16) & 0x1f; |
566
|
|
|
$mon = ($timestamp >> 21) & 0x0f; |
567
|
|
|
$year = ($timestamp >> 25) & 0x7f; |
568
|
|
|
return mktime($hrs, $min, $sec, $mon, $day, $year + 1980); |
569
|
|
|
} |
570
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.