|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HexMakina\Tempus; |
|
4
|
|
|
|
|
5
|
|
|
class Dato extends Base |
|
6
|
|
|
{ |
|
7
|
|
|
const FORMAT = 'Y-m-d'; |
|
8
|
|
|
const FORMAT_YEAR = 'Y'; |
|
9
|
|
|
|
|
10
|
|
|
const DAYS_IN_A_WEEK = 7; |
|
11
|
|
|
const WEEKS_IN_A_MONTH = 4; |
|
12
|
|
|
const MONTHES_IN_A_YEAR = 12; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
public static function format($parm = null, $format = null) |
|
16
|
|
|
{ |
|
17
|
|
|
$format = $format ?? self::FORMAT; |
|
18
|
|
|
return parent::format($parm, $format); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public static function today($format = null) |
|
22
|
|
|
{ |
|
23
|
|
|
$format = $format ?? self::FORMAT; |
|
24
|
|
|
return parent::format(null, $format); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
// find the total numbers of days in full years since the mythical 1/1/0001, then add the number of days before the current one in the year passed. Do this for each date, then return the absolute value of the difference |
|
28
|
|
|
public static function days_diff($a, $b, $c = 'DateTime') |
|
29
|
|
|
{ |
|
30
|
|
|
if (!is_a($a, $c) || !is_a($b, $c)) { |
|
31
|
|
|
throw new \InvalidArgumentException('ARGUMENT_MUST_HAVE_SAME_TYPE'); |
|
32
|
|
|
} |
|
33
|
|
|
return self::days($a) - self::days($b); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function days($x, $d = 0) |
|
37
|
|
|
{ |
|
38
|
|
|
$lfy = $x->format('Y') - 1; // last full year |
|
39
|
|
|
|
|
40
|
|
|
foreach ([4, -100, 400] as $f) { // leap year factors |
|
41
|
|
|
$d += ($lfy / $f); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return (int)(($lfy * 365.25) + $d + $x->format('z')); // days since 1.1.1001 from last year (+|-) leap days + days in current year |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function days_diff_in_parts($amount_of_days) |
|
48
|
|
|
{ |
|
49
|
|
|
$date_diff = []; |
|
50
|
|
|
|
|
51
|
|
|
$previous_part = 'd'; |
|
52
|
|
|
|
|
53
|
|
|
foreach (['w' => self::DAYS_IN_A_WEEK, 'm' => self::WEEKS_IN_A_MONTH, 'y' => self::MONTHES_IN_A_YEAR] as $part => $limit) { |
|
54
|
|
|
if ($amount_of_days >= $limit) { |
|
55
|
|
|
$date_diff[$part] = intval($amount_of_days / $limit); |
|
56
|
|
|
$date_diff[$previous_part] = $amount_of_days % $limit; |
|
57
|
|
|
$previous_part = $part; |
|
58
|
|
|
$amount_of_days = intval($amount_of_days / $limit); |
|
59
|
|
|
} else { |
|
60
|
|
|
$date_diff[$previous_part] = $amount_of_days; |
|
61
|
|
|
break; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $date_diff; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public static function first_date($year, $month, $format = null) |
|
69
|
|
|
{ |
|
70
|
|
|
$format = $format ?? self::FORMAT; |
|
71
|
|
|
return date_format(new \DateTime("$year-$month-1"), $format); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public static function last_date($year, $month, $format = null) |
|
75
|
|
|
{ |
|
76
|
|
|
$format = $format ?? self::FORMAT; |
|
77
|
|
|
return date_format(new \DateTime("$year-$month-" . self::last_day($year, $month)), $format); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public static function last_day($year, $month) |
|
81
|
|
|
{ |
|
82
|
|
|
return date_format(new \DateTime("$year-$month-1"), 't'); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|