1 | <?php |
||
24 | final class DateInterval |
||
25 | { |
||
26 | /** |
||
27 | * PHP DateInverval object |
||
28 | * |
||
29 | * @var \DateInterval |
||
30 | * @since 2.0.0 |
||
31 | */ |
||
32 | private $interval; |
||
33 | |||
34 | /** |
||
35 | * Constructor. |
||
36 | * |
||
37 | * @param \DateInterval|string $interval Either a PHP DateInterval object or a string with an interval specification. |
||
38 | * |
||
39 | * @since 2.0.0 |
||
40 | */ |
||
41 | 303 | public function __construct($interval) |
|
45 | |||
46 | /** |
||
47 | * Creates a DateInterval object from the relative parts of the string. |
||
48 | * |
||
49 | * @param string $time A date with relative parts. |
||
50 | * |
||
51 | * @return DateInterval |
||
52 | * |
||
53 | * @since 2.0.0 |
||
54 | */ |
||
55 | 8 | public static function createFromDateString($time) |
|
59 | |||
60 | /** |
||
61 | * Creates a DateInterval object by adding another interval into it. |
||
62 | * Uses absolute values for addition process. |
||
63 | * |
||
64 | * @param DateInterval $interval The interval to be added. |
||
65 | * |
||
66 | * @return DateInterval |
||
67 | * |
||
68 | * @since 2.0.0 |
||
69 | */ |
||
70 | 6 | public function add(DateInterval $interval) |
|
71 | { |
||
72 | 6 | $years = $this->y + $interval->y; |
|
73 | 6 | $months = $this->m + $interval->m; |
|
74 | 6 | $days = $this->d + $interval->d; |
|
75 | 6 | $hours = $this->h + $interval->h; |
|
76 | 6 | $minutes = $this->i + $interval->i; |
|
77 | 6 | $seconds = $this->s + $interval->s; |
|
78 | |||
79 | 6 | $spec = sprintf('P%sY%sM%sDT%sH%sM%sS', $years, $months, $days, $hours, $minutes, $seconds); |
|
80 | |||
81 | 6 | return new DateInterval($spec); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Checks if the current interval is equals to the interval given as parameter. |
||
86 | * |
||
87 | * @param DateInterval $interval The interval to compare. |
||
88 | * |
||
89 | * @return boolean |
||
90 | * |
||
91 | * @since 2.0.0 |
||
92 | */ |
||
93 | 18 | public function equals(DateInterval $interval) |
|
94 | { |
||
95 | 18 | $years = $this->y == $interval->y; |
|
96 | 18 | $months = $this->m == $interval->m; |
|
97 | 18 | $days = $this->d == $interval->d; |
|
98 | 18 | $hours = $this->h == $interval->h; |
|
99 | 18 | $minutes = $this->i == $interval->i; |
|
100 | 18 | $seconds = $this->s == $interval->s; |
|
101 | 18 | $invert = $this->invert == $interval->invert; |
|
102 | |||
103 | 18 | return $years && $months && $days && $hours && $minutes && $seconds && $invert; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * Creates a DateInterval object by inverting the value of the current one. |
||
108 | * |
||
109 | * @return DateInterval |
||
110 | * |
||
111 | * @since 2.0.0 |
||
112 | */ |
||
113 | 2 | public function invert() |
|
114 | { |
||
115 | 2 | $interval = $this->copy($this->interval); |
|
116 | 2 | $interval->invert = $interval->invert ? 0 : 1; |
|
117 | |||
118 | 2 | return new DateInterval($interval); |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Formats the interval. |
||
123 | * |
||
124 | * @param string $format Format accepted by PHP DateInterval::format(). |
||
125 | * |
||
126 | * @return string |
||
127 | * |
||
128 | * @since 2.0.0 |
||
129 | */ |
||
130 | 143 | public function format($format) |
|
131 | { |
||
132 | 143 | return $this->interval->format($format); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Magic method to access properties of the interval. |
||
137 | * |
||
138 | * @param string $name The name of the property. |
||
139 | * |
||
140 | * @return mixed |
||
141 | * |
||
142 | * @since 2.0.0 |
||
143 | */ |
||
144 | 31 | public function __get($name) |
|
148 | |||
149 | /** |
||
150 | * Returns a PHP DateInterval object. |
||
151 | * |
||
152 | * @return \DateInterval |
||
153 | * |
||
154 | * @since 2.0.0 |
||
155 | */ |
||
156 | 164 | public function getDateInterval() |
|
160 | |||
161 | /** |
||
162 | * Creates a copy of PHP DateInterval object. |
||
163 | * |
||
164 | * @param \DateInterval $interval The object to copy. |
||
165 | * |
||
166 | * @return \DateInterval |
||
167 | * |
||
168 | * @since 2.0.0 |
||
169 | */ |
||
170 | 314 | private function copy(\DateInterval $interval) |
|
177 | } |
||
178 |