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