Completed
Push — master ( 9802ab...25d319 )
by Lorenzo
03:18
created

datetime.php ➔ diff_in_year()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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.

Loading history...
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
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

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.

Loading history...
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