@@ -21,51 +21,51 @@ |
||
21 | 21 | */ |
22 | 22 | trait Mutability |
23 | 23 | { |
24 | - use Cast; |
|
24 | + use Cast; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Returns true if the current class/instance is mutable. |
|
28 | - * |
|
29 | - * @return bool |
|
30 | - */ |
|
31 | - public static function isMutable() |
|
32 | - { |
|
33 | - return false; |
|
34 | - } |
|
26 | + /** |
|
27 | + * Returns true if the current class/instance is mutable. |
|
28 | + * |
|
29 | + * @return bool |
|
30 | + */ |
|
31 | + public static function isMutable() |
|
32 | + { |
|
33 | + return false; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Returns true if the current class/instance is immutable. |
|
38 | - * |
|
39 | - * @return bool |
|
40 | - */ |
|
41 | - public static function isImmutable() |
|
42 | - { |
|
43 | - return !static::isMutable(); |
|
44 | - } |
|
36 | + /** |
|
37 | + * Returns true if the current class/instance is immutable. |
|
38 | + * |
|
39 | + * @return bool |
|
40 | + */ |
|
41 | + public static function isImmutable() |
|
42 | + { |
|
43 | + return !static::isMutable(); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Return a mutable copy of the instance. |
|
48 | - * |
|
49 | - * @return Carbon |
|
50 | - */ |
|
51 | - public function toMutable() |
|
52 | - { |
|
53 | - /** @var Carbon $date */ |
|
54 | - $date = $this->cast(Carbon::class); |
|
46 | + /** |
|
47 | + * Return a mutable copy of the instance. |
|
48 | + * |
|
49 | + * @return Carbon |
|
50 | + */ |
|
51 | + public function toMutable() |
|
52 | + { |
|
53 | + /** @var Carbon $date */ |
|
54 | + $date = $this->cast(Carbon::class); |
|
55 | 55 | |
56 | - return $date; |
|
57 | - } |
|
56 | + return $date; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Return a immutable copy of the instance. |
|
61 | - * |
|
62 | - * @return CarbonImmutable |
|
63 | - */ |
|
64 | - public function toImmutable() |
|
65 | - { |
|
66 | - /** @var CarbonImmutable $date */ |
|
67 | - $date = $this->cast(CarbonImmutable::class); |
|
59 | + /** |
|
60 | + * Return a immutable copy of the instance. |
|
61 | + * |
|
62 | + * @return CarbonImmutable |
|
63 | + */ |
|
64 | + public function toImmutable() |
|
65 | + { |
|
66 | + /** @var CarbonImmutable $date */ |
|
67 | + $date = $this->cast(CarbonImmutable::class); |
|
68 | 68 | |
69 | - return $date; |
|
70 | - } |
|
69 | + return $date; |
|
70 | + } |
|
71 | 71 | } |
@@ -26,446 +26,446 @@ |
||
26 | 26 | */ |
27 | 27 | trait Options |
28 | 28 | { |
29 | - use Localization; |
|
30 | - |
|
31 | - /** |
|
32 | - * Customizable PHP_INT_SIZE override. |
|
33 | - * |
|
34 | - * @var int |
|
35 | - */ |
|
36 | - public static $PHPIntSize = PHP_INT_SIZE; |
|
37 | - |
|
38 | - /** |
|
39 | - * First day of week. |
|
40 | - * |
|
41 | - * @var int|string |
|
42 | - */ |
|
43 | - protected static $weekStartsAt = CarbonInterface::MONDAY; |
|
44 | - |
|
45 | - /** |
|
46 | - * Last day of week. |
|
47 | - * |
|
48 | - * @var int|string |
|
49 | - */ |
|
50 | - protected static $weekEndsAt = CarbonInterface::SUNDAY; |
|
51 | - |
|
52 | - /** |
|
53 | - * Days of weekend. |
|
54 | - * |
|
55 | - * @var array |
|
56 | - */ |
|
57 | - protected static $weekendDays = [ |
|
58 | - CarbonInterface::SATURDAY, |
|
59 | - CarbonInterface::SUNDAY, |
|
60 | - ]; |
|
61 | - |
|
62 | - /** |
|
63 | - * Format regex patterns. |
|
64 | - * |
|
65 | - * @var array<string, string> |
|
66 | - */ |
|
67 | - protected static $regexFormats = [ |
|
68 | - 'd' => '(3[01]|[12][0-9]|0[1-9])', |
|
69 | - 'D' => '(Sun|Mon|Tue|Wed|Thu|Fri|Sat)', |
|
70 | - 'j' => '([123][0-9]|[1-9])', |
|
71 | - 'l' => '([a-zA-Z]{2,})', |
|
72 | - 'N' => '([1-7])', |
|
73 | - 'S' => '(st|nd|rd|th)', |
|
74 | - 'w' => '([0-6])', |
|
75 | - 'z' => '(36[0-5]|3[0-5][0-9]|[12][0-9]{2}|[1-9]?[0-9])', |
|
76 | - 'W' => '(5[012]|[1-4][0-9]|0?[1-9])', |
|
77 | - 'F' => '([a-zA-Z]{2,})', |
|
78 | - 'm' => '(1[012]|0[1-9])', |
|
79 | - 'M' => '([a-zA-Z]{3})', |
|
80 | - 'n' => '(1[012]|[1-9])', |
|
81 | - 't' => '(2[89]|3[01])', |
|
82 | - 'L' => '(0|1)', |
|
83 | - 'o' => '([1-9][0-9]{0,4})', |
|
84 | - 'Y' => '([1-9]?[0-9]{4})', |
|
85 | - 'y' => '([0-9]{2})', |
|
86 | - 'a' => '(am|pm)', |
|
87 | - 'A' => '(AM|PM)', |
|
88 | - 'B' => '([0-9]{3})', |
|
89 | - 'g' => '(1[012]|[1-9])', |
|
90 | - 'G' => '(2[0-3]|1?[0-9])', |
|
91 | - 'h' => '(1[012]|0[1-9])', |
|
92 | - 'H' => '(2[0-3]|[01][0-9])', |
|
93 | - 'i' => '([0-5][0-9])', |
|
94 | - 's' => '([0-5][0-9])', |
|
95 | - 'u' => '([0-9]{1,6})', |
|
96 | - 'v' => '([0-9]{1,3})', |
|
97 | - 'e' => '([a-zA-Z]{1,5})|([a-zA-Z]*\\/[a-zA-Z]*)', |
|
98 | - 'I' => '(0|1)', |
|
99 | - 'O' => '([+-](1[012]|0[0-9])[0134][05])', |
|
100 | - 'P' => '([+-](1[012]|0[0-9]):[0134][05])', |
|
101 | - 'p' => '(Z|[+-](1[012]|0[0-9]):[0134][05])', |
|
102 | - 'T' => '([a-zA-Z]{1,5})', |
|
103 | - 'Z' => '(-?[1-5]?[0-9]{1,4})', |
|
104 | - 'U' => '([0-9]*)', |
|
105 | - |
|
106 | - // The formats below are combinations of the above formats. |
|
107 | - 'c' => '(([1-9]?[0-9]{4})-(1[012]|0[1-9])-(3[01]|[12][0-9]|0[1-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])[+-](1[012]|0[0-9]):([0134][05]))', // Y-m-dTH:i:sP |
|
108 | - 'r' => '(([a-zA-Z]{3}), ([123][0-9]|0[1-9]) ([a-zA-Z]{3}) ([1-9]?[0-9]{4}) (2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9]) [+-](1[012]|0[0-9])([0134][05]))', // D, d M Y H:i:s O |
|
109 | - ]; |
|
110 | - |
|
111 | - /** |
|
112 | - * Format modifiers (such as available in createFromFormat) regex patterns. |
|
113 | - * |
|
114 | - * @var array |
|
115 | - */ |
|
116 | - protected static $regexFormatModifiers = [ |
|
117 | - '*' => '.+', |
|
118 | - ' ' => '[ ]', |
|
119 | - '#' => '[;:\\/.,()-]', |
|
120 | - '?' => '([^a]|[a])', |
|
121 | - '!' => '', |
|
122 | - '|' => '', |
|
123 | - '+' => '', |
|
124 | - ]; |
|
125 | - |
|
126 | - /** |
|
127 | - * Indicates if months should be calculated with overflow. |
|
128 | - * Global setting. |
|
129 | - * |
|
130 | - * @var bool |
|
131 | - */ |
|
132 | - protected static $monthsOverflow = true; |
|
133 | - |
|
134 | - /** |
|
135 | - * Indicates if years should be calculated with overflow. |
|
136 | - * Global setting. |
|
137 | - * |
|
138 | - * @var bool |
|
139 | - */ |
|
140 | - protected static $yearsOverflow = true; |
|
141 | - |
|
142 | - /** |
|
143 | - * Indicates if the strict mode is in use. |
|
144 | - * Global setting. |
|
145 | - * |
|
146 | - * @var bool |
|
147 | - */ |
|
148 | - protected static $strictModeEnabled = true; |
|
149 | - |
|
150 | - /** |
|
151 | - * Function to call instead of format. |
|
152 | - * |
|
153 | - * @var string|callable|null |
|
154 | - */ |
|
155 | - protected static $formatFunction; |
|
156 | - |
|
157 | - /** |
|
158 | - * Function to call instead of createFromFormat. |
|
159 | - * |
|
160 | - * @var string|callable|null |
|
161 | - */ |
|
162 | - protected static $createFromFormatFunction; |
|
163 | - |
|
164 | - /** |
|
165 | - * Function to call instead of parse. |
|
166 | - * |
|
167 | - * @var string|callable|null |
|
168 | - */ |
|
169 | - protected static $parseFunction; |
|
170 | - |
|
171 | - /** |
|
172 | - * Indicates if months should be calculated with overflow. |
|
173 | - * Specific setting. |
|
174 | - * |
|
175 | - * @var bool|null |
|
176 | - */ |
|
177 | - protected $localMonthsOverflow; |
|
178 | - |
|
179 | - /** |
|
180 | - * Indicates if years should be calculated with overflow. |
|
181 | - * Specific setting. |
|
182 | - * |
|
183 | - * @var bool|null |
|
184 | - */ |
|
185 | - protected $localYearsOverflow; |
|
186 | - |
|
187 | - /** |
|
188 | - * Indicates if the strict mode is in use. |
|
189 | - * Specific setting. |
|
190 | - * |
|
191 | - * @var bool|null |
|
192 | - */ |
|
193 | - protected $localStrictModeEnabled; |
|
194 | - |
|
195 | - /** |
|
196 | - * Options for diffForHumans and forHumans methods. |
|
197 | - * |
|
198 | - * @var bool|null |
|
199 | - */ |
|
200 | - protected $localHumanDiffOptions; |
|
201 | - |
|
202 | - /** |
|
203 | - * Format to use on string cast. |
|
204 | - * |
|
205 | - * @var string|null |
|
206 | - */ |
|
207 | - protected $localToStringFormat; |
|
208 | - |
|
209 | - /** |
|
210 | - * Format to use on JSON serialization. |
|
211 | - * |
|
212 | - * @var string|null |
|
213 | - */ |
|
214 | - protected $localSerializer; |
|
215 | - |
|
216 | - /** |
|
217 | - * Instance-specific macros. |
|
218 | - * |
|
219 | - * @var array|null |
|
220 | - */ |
|
221 | - protected $localMacros; |
|
222 | - |
|
223 | - /** |
|
224 | - * Instance-specific generic macros. |
|
225 | - * |
|
226 | - * @var array|null |
|
227 | - */ |
|
228 | - protected $localGenericMacros; |
|
229 | - |
|
230 | - /** |
|
231 | - * Function to call instead of format. |
|
232 | - * |
|
233 | - * @var string|callable|null |
|
234 | - */ |
|
235 | - protected $localFormatFunction; |
|
236 | - |
|
237 | - /** |
|
238 | - * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
239 | - * You should rather use the ->settings() method. |
|
240 | - * @see settings |
|
241 | - * |
|
242 | - * Enable the strict mode (or disable with passing false). |
|
243 | - * |
|
244 | - * @param bool $strictModeEnabled |
|
245 | - */ |
|
246 | - public static function useStrictMode($strictModeEnabled = true) |
|
247 | - { |
|
248 | - static::$strictModeEnabled = $strictModeEnabled; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Returns true if the strict mode is globally in use, false else. |
|
253 | - * (It can be overridden in specific instances.) |
|
254 | - * |
|
255 | - * @return bool |
|
256 | - */ |
|
257 | - public static function isStrictModeEnabled() |
|
258 | - { |
|
259 | - return static::$strictModeEnabled; |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
264 | - * You should rather use the ->settings() method. |
|
265 | - * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants |
|
266 | - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). |
|
267 | - * @see settings |
|
268 | - * |
|
269 | - * Indicates if months should be calculated with overflow. |
|
270 | - * |
|
271 | - * @param bool $monthsOverflow |
|
272 | - * |
|
273 | - * @return void |
|
274 | - */ |
|
275 | - public static function useMonthsOverflow($monthsOverflow = true) |
|
276 | - { |
|
277 | - static::$monthsOverflow = $monthsOverflow; |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
282 | - * You should rather use the ->settings() method. |
|
283 | - * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants |
|
284 | - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). |
|
285 | - * @see settings |
|
286 | - * |
|
287 | - * Reset the month overflow behavior. |
|
288 | - * |
|
289 | - * @return void |
|
290 | - */ |
|
291 | - public static function resetMonthsOverflow() |
|
292 | - { |
|
293 | - static::$monthsOverflow = true; |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Get the month overflow global behavior (can be overridden in specific instances). |
|
298 | - * |
|
299 | - * @return bool |
|
300 | - */ |
|
301 | - public static function shouldOverflowMonths() |
|
302 | - { |
|
303 | - return static::$monthsOverflow; |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
308 | - * You should rather use the ->settings() method. |
|
309 | - * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants |
|
310 | - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). |
|
311 | - * @see settings |
|
312 | - * |
|
313 | - * Indicates if years should be calculated with overflow. |
|
314 | - * |
|
315 | - * @param bool $yearsOverflow |
|
316 | - * |
|
317 | - * @return void |
|
318 | - */ |
|
319 | - public static function useYearsOverflow($yearsOverflow = true) |
|
320 | - { |
|
321 | - static::$yearsOverflow = $yearsOverflow; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
326 | - * You should rather use the ->settings() method. |
|
327 | - * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants |
|
328 | - * are available for quarters, years, decade, centuries, millennia (singular and plural forms). |
|
329 | - * @see settings |
|
330 | - * |
|
331 | - * Reset the month overflow behavior. |
|
332 | - * |
|
333 | - * @return void |
|
334 | - */ |
|
335 | - public static function resetYearsOverflow() |
|
336 | - { |
|
337 | - static::$yearsOverflow = true; |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Get the month overflow global behavior (can be overridden in specific instances). |
|
342 | - * |
|
343 | - * @return bool |
|
344 | - */ |
|
345 | - public static function shouldOverflowYears() |
|
346 | - { |
|
347 | - return static::$yearsOverflow; |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * Set specific options. |
|
352 | - * - strictMode: true|false|null |
|
353 | - * - monthOverflow: true|false|null |
|
354 | - * - yearOverflow: true|false|null |
|
355 | - * - humanDiffOptions: int|null |
|
356 | - * - toStringFormat: string|Closure|null |
|
357 | - * - toJsonFormat: string|Closure|null |
|
358 | - * - locale: string|null |
|
359 | - * - timezone: \DateTimeZone|string|int|null |
|
360 | - * - macros: array|null |
|
361 | - * - genericMacros: array|null |
|
362 | - * |
|
363 | - * @param array $settings |
|
364 | - * |
|
365 | - * @return $this|static |
|
366 | - */ |
|
367 | - public function settings(array $settings) |
|
368 | - { |
|
369 | - $this->localStrictModeEnabled = $settings['strictMode'] ?? null; |
|
370 | - $this->localMonthsOverflow = $settings['monthOverflow'] ?? null; |
|
371 | - $this->localYearsOverflow = $settings['yearOverflow'] ?? null; |
|
372 | - $this->localHumanDiffOptions = $settings['humanDiffOptions'] ?? null; |
|
373 | - $this->localToStringFormat = $settings['toStringFormat'] ?? null; |
|
374 | - $this->localSerializer = $settings['toJsonFormat'] ?? null; |
|
375 | - $this->localMacros = $settings['macros'] ?? null; |
|
376 | - $this->localGenericMacros = $settings['genericMacros'] ?? null; |
|
377 | - $this->localFormatFunction = $settings['formatFunction'] ?? null; |
|
378 | - |
|
379 | - if (isset($settings['locale'])) { |
|
380 | - $locales = $settings['locale']; |
|
381 | - |
|
382 | - if (!\is_array($locales)) { |
|
383 | - $locales = [$locales]; |
|
384 | - } |
|
385 | - |
|
386 | - $this->locale(...$locales); |
|
387 | - } |
|
388 | - |
|
389 | - if (isset($settings['innerTimezone'])) { |
|
390 | - return $this->setTimezone($settings['innerTimezone']); |
|
391 | - } |
|
392 | - |
|
393 | - if (isset($settings['timezone'])) { |
|
394 | - return $this->shiftTimezone($settings['timezone']); |
|
395 | - } |
|
396 | - |
|
397 | - return $this; |
|
398 | - } |
|
399 | - |
|
400 | - /** |
|
401 | - * Returns current local settings. |
|
402 | - * |
|
403 | - * @return array |
|
404 | - */ |
|
405 | - public function getSettings() |
|
406 | - { |
|
407 | - $settings = []; |
|
408 | - $map = [ |
|
409 | - 'localStrictModeEnabled' => 'strictMode', |
|
410 | - 'localMonthsOverflow' => 'monthOverflow', |
|
411 | - 'localYearsOverflow' => 'yearOverflow', |
|
412 | - 'localHumanDiffOptions' => 'humanDiffOptions', |
|
413 | - 'localToStringFormat' => 'toStringFormat', |
|
414 | - 'localSerializer' => 'toJsonFormat', |
|
415 | - 'localMacros' => 'macros', |
|
416 | - 'localGenericMacros' => 'genericMacros', |
|
417 | - 'locale' => 'locale', |
|
418 | - 'tzName' => 'timezone', |
|
419 | - 'localFormatFunction' => 'formatFunction', |
|
420 | - ]; |
|
421 | - |
|
422 | - foreach ($map as $property => $key) { |
|
423 | - $value = $this->$property ?? null; |
|
424 | - |
|
425 | - if ($value !== null) { |
|
426 | - $settings[$key] = $value; |
|
427 | - } |
|
428 | - } |
|
429 | - |
|
430 | - return $settings; |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * Show truthy properties on var_dump(). |
|
435 | - * |
|
436 | - * @return array |
|
437 | - */ |
|
438 | - public function __debugInfo() |
|
439 | - { |
|
440 | - $infos = array_filter(get_object_vars($this), function ($var) { |
|
441 | - return $var; |
|
442 | - }); |
|
443 | - |
|
444 | - foreach (['dumpProperties', 'constructedObjectId'] as $property) { |
|
445 | - if (isset($infos[$property])) { |
|
446 | - unset($infos[$property]); |
|
447 | - } |
|
448 | - } |
|
449 | - |
|
450 | - $this->addExtraDebugInfos($infos); |
|
451 | - |
|
452 | - return $infos; |
|
453 | - } |
|
454 | - |
|
455 | - protected function addExtraDebugInfos(&$infos): void |
|
456 | - { |
|
457 | - if ($this instanceof DateTimeInterface) { |
|
458 | - try { |
|
459 | - if (!isset($infos['date'])) { |
|
460 | - $infos['date'] = $this->format(CarbonInterface::MOCK_DATETIME_FORMAT); |
|
461 | - } |
|
462 | - |
|
463 | - if (!isset($infos['timezone'])) { |
|
464 | - $infos['timezone'] = $this->tzName; |
|
465 | - } |
|
466 | - } catch (Throwable $exception) { |
|
467 | - // noop |
|
468 | - } |
|
469 | - } |
|
470 | - } |
|
29 | + use Localization; |
|
30 | + |
|
31 | + /** |
|
32 | + * Customizable PHP_INT_SIZE override. |
|
33 | + * |
|
34 | + * @var int |
|
35 | + */ |
|
36 | + public static $PHPIntSize = PHP_INT_SIZE; |
|
37 | + |
|
38 | + /** |
|
39 | + * First day of week. |
|
40 | + * |
|
41 | + * @var int|string |
|
42 | + */ |
|
43 | + protected static $weekStartsAt = CarbonInterface::MONDAY; |
|
44 | + |
|
45 | + /** |
|
46 | + * Last day of week. |
|
47 | + * |
|
48 | + * @var int|string |
|
49 | + */ |
|
50 | + protected static $weekEndsAt = CarbonInterface::SUNDAY; |
|
51 | + |
|
52 | + /** |
|
53 | + * Days of weekend. |
|
54 | + * |
|
55 | + * @var array |
|
56 | + */ |
|
57 | + protected static $weekendDays = [ |
|
58 | + CarbonInterface::SATURDAY, |
|
59 | + CarbonInterface::SUNDAY, |
|
60 | + ]; |
|
61 | + |
|
62 | + /** |
|
63 | + * Format regex patterns. |
|
64 | + * |
|
65 | + * @var array<string, string> |
|
66 | + */ |
|
67 | + protected static $regexFormats = [ |
|
68 | + 'd' => '(3[01]|[12][0-9]|0[1-9])', |
|
69 | + 'D' => '(Sun|Mon|Tue|Wed|Thu|Fri|Sat)', |
|
70 | + 'j' => '([123][0-9]|[1-9])', |
|
71 | + 'l' => '([a-zA-Z]{2,})', |
|
72 | + 'N' => '([1-7])', |
|
73 | + 'S' => '(st|nd|rd|th)', |
|
74 | + 'w' => '([0-6])', |
|
75 | + 'z' => '(36[0-5]|3[0-5][0-9]|[12][0-9]{2}|[1-9]?[0-9])', |
|
76 | + 'W' => '(5[012]|[1-4][0-9]|0?[1-9])', |
|
77 | + 'F' => '([a-zA-Z]{2,})', |
|
78 | + 'm' => '(1[012]|0[1-9])', |
|
79 | + 'M' => '([a-zA-Z]{3})', |
|
80 | + 'n' => '(1[012]|[1-9])', |
|
81 | + 't' => '(2[89]|3[01])', |
|
82 | + 'L' => '(0|1)', |
|
83 | + 'o' => '([1-9][0-9]{0,4})', |
|
84 | + 'Y' => '([1-9]?[0-9]{4})', |
|
85 | + 'y' => '([0-9]{2})', |
|
86 | + 'a' => '(am|pm)', |
|
87 | + 'A' => '(AM|PM)', |
|
88 | + 'B' => '([0-9]{3})', |
|
89 | + 'g' => '(1[012]|[1-9])', |
|
90 | + 'G' => '(2[0-3]|1?[0-9])', |
|
91 | + 'h' => '(1[012]|0[1-9])', |
|
92 | + 'H' => '(2[0-3]|[01][0-9])', |
|
93 | + 'i' => '([0-5][0-9])', |
|
94 | + 's' => '([0-5][0-9])', |
|
95 | + 'u' => '([0-9]{1,6})', |
|
96 | + 'v' => '([0-9]{1,3})', |
|
97 | + 'e' => '([a-zA-Z]{1,5})|([a-zA-Z]*\\/[a-zA-Z]*)', |
|
98 | + 'I' => '(0|1)', |
|
99 | + 'O' => '([+-](1[012]|0[0-9])[0134][05])', |
|
100 | + 'P' => '([+-](1[012]|0[0-9]):[0134][05])', |
|
101 | + 'p' => '(Z|[+-](1[012]|0[0-9]):[0134][05])', |
|
102 | + 'T' => '([a-zA-Z]{1,5})', |
|
103 | + 'Z' => '(-?[1-5]?[0-9]{1,4})', |
|
104 | + 'U' => '([0-9]*)', |
|
105 | + |
|
106 | + // The formats below are combinations of the above formats. |
|
107 | + 'c' => '(([1-9]?[0-9]{4})-(1[012]|0[1-9])-(3[01]|[12][0-9]|0[1-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])[+-](1[012]|0[0-9]):([0134][05]))', // Y-m-dTH:i:sP |
|
108 | + 'r' => '(([a-zA-Z]{3}), ([123][0-9]|0[1-9]) ([a-zA-Z]{3}) ([1-9]?[0-9]{4}) (2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9]) [+-](1[012]|0[0-9])([0134][05]))', // D, d M Y H:i:s O |
|
109 | + ]; |
|
110 | + |
|
111 | + /** |
|
112 | + * Format modifiers (such as available in createFromFormat) regex patterns. |
|
113 | + * |
|
114 | + * @var array |
|
115 | + */ |
|
116 | + protected static $regexFormatModifiers = [ |
|
117 | + '*' => '.+', |
|
118 | + ' ' => '[ ]', |
|
119 | + '#' => '[;:\\/.,()-]', |
|
120 | + '?' => '([^a]|[a])', |
|
121 | + '!' => '', |
|
122 | + '|' => '', |
|
123 | + '+' => '', |
|
124 | + ]; |
|
125 | + |
|
126 | + /** |
|
127 | + * Indicates if months should be calculated with overflow. |
|
128 | + * Global setting. |
|
129 | + * |
|
130 | + * @var bool |
|
131 | + */ |
|
132 | + protected static $monthsOverflow = true; |
|
133 | + |
|
134 | + /** |
|
135 | + * Indicates if years should be calculated with overflow. |
|
136 | + * Global setting. |
|
137 | + * |
|
138 | + * @var bool |
|
139 | + */ |
|
140 | + protected static $yearsOverflow = true; |
|
141 | + |
|
142 | + /** |
|
143 | + * Indicates if the strict mode is in use. |
|
144 | + * Global setting. |
|
145 | + * |
|
146 | + * @var bool |
|
147 | + */ |
|
148 | + protected static $strictModeEnabled = true; |
|
149 | + |
|
150 | + /** |
|
151 | + * Function to call instead of format. |
|
152 | + * |
|
153 | + * @var string|callable|null |
|
154 | + */ |
|
155 | + protected static $formatFunction; |
|
156 | + |
|
157 | + /** |
|
158 | + * Function to call instead of createFromFormat. |
|
159 | + * |
|
160 | + * @var string|callable|null |
|
161 | + */ |
|
162 | + protected static $createFromFormatFunction; |
|
163 | + |
|
164 | + /** |
|
165 | + * Function to call instead of parse. |
|
166 | + * |
|
167 | + * @var string|callable|null |
|
168 | + */ |
|
169 | + protected static $parseFunction; |
|
170 | + |
|
171 | + /** |
|
172 | + * Indicates if months should be calculated with overflow. |
|
173 | + * Specific setting. |
|
174 | + * |
|
175 | + * @var bool|null |
|
176 | + */ |
|
177 | + protected $localMonthsOverflow; |
|
178 | + |
|
179 | + /** |
|
180 | + * Indicates if years should be calculated with overflow. |
|
181 | + * Specific setting. |
|
182 | + * |
|
183 | + * @var bool|null |
|
184 | + */ |
|
185 | + protected $localYearsOverflow; |
|
186 | + |
|
187 | + /** |
|
188 | + * Indicates if the strict mode is in use. |
|
189 | + * Specific setting. |
|
190 | + * |
|
191 | + * @var bool|null |
|
192 | + */ |
|
193 | + protected $localStrictModeEnabled; |
|
194 | + |
|
195 | + /** |
|
196 | + * Options for diffForHumans and forHumans methods. |
|
197 | + * |
|
198 | + * @var bool|null |
|
199 | + */ |
|
200 | + protected $localHumanDiffOptions; |
|
201 | + |
|
202 | + /** |
|
203 | + * Format to use on string cast. |
|
204 | + * |
|
205 | + * @var string|null |
|
206 | + */ |
|
207 | + protected $localToStringFormat; |
|
208 | + |
|
209 | + /** |
|
210 | + * Format to use on JSON serialization. |
|
211 | + * |
|
212 | + * @var string|null |
|
213 | + */ |
|
214 | + protected $localSerializer; |
|
215 | + |
|
216 | + /** |
|
217 | + * Instance-specific macros. |
|
218 | + * |
|
219 | + * @var array|null |
|
220 | + */ |
|
221 | + protected $localMacros; |
|
222 | + |
|
223 | + /** |
|
224 | + * Instance-specific generic macros. |
|
225 | + * |
|
226 | + * @var array|null |
|
227 | + */ |
|
228 | + protected $localGenericMacros; |
|
229 | + |
|
230 | + /** |
|
231 | + * Function to call instead of format. |
|
232 | + * |
|
233 | + * @var string|callable|null |
|
234 | + */ |
|
235 | + protected $localFormatFunction; |
|
236 | + |
|
237 | + /** |
|
238 | + * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
239 | + * You should rather use the ->settings() method. |
|
240 | + * @see settings |
|
241 | + * |
|
242 | + * Enable the strict mode (or disable with passing false). |
|
243 | + * |
|
244 | + * @param bool $strictModeEnabled |
|
245 | + */ |
|
246 | + public static function useStrictMode($strictModeEnabled = true) |
|
247 | + { |
|
248 | + static::$strictModeEnabled = $strictModeEnabled; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Returns true if the strict mode is globally in use, false else. |
|
253 | + * (It can be overridden in specific instances.) |
|
254 | + * |
|
255 | + * @return bool |
|
256 | + */ |
|
257 | + public static function isStrictModeEnabled() |
|
258 | + { |
|
259 | + return static::$strictModeEnabled; |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
264 | + * You should rather use the ->settings() method. |
|
265 | + * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants |
|
266 | + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). |
|
267 | + * @see settings |
|
268 | + * |
|
269 | + * Indicates if months should be calculated with overflow. |
|
270 | + * |
|
271 | + * @param bool $monthsOverflow |
|
272 | + * |
|
273 | + * @return void |
|
274 | + */ |
|
275 | + public static function useMonthsOverflow($monthsOverflow = true) |
|
276 | + { |
|
277 | + static::$monthsOverflow = $monthsOverflow; |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
282 | + * You should rather use the ->settings() method. |
|
283 | + * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants |
|
284 | + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). |
|
285 | + * @see settings |
|
286 | + * |
|
287 | + * Reset the month overflow behavior. |
|
288 | + * |
|
289 | + * @return void |
|
290 | + */ |
|
291 | + public static function resetMonthsOverflow() |
|
292 | + { |
|
293 | + static::$monthsOverflow = true; |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Get the month overflow global behavior (can be overridden in specific instances). |
|
298 | + * |
|
299 | + * @return bool |
|
300 | + */ |
|
301 | + public static function shouldOverflowMonths() |
|
302 | + { |
|
303 | + return static::$monthsOverflow; |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
308 | + * You should rather use the ->settings() method. |
|
309 | + * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants |
|
310 | + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). |
|
311 | + * @see settings |
|
312 | + * |
|
313 | + * Indicates if years should be calculated with overflow. |
|
314 | + * |
|
315 | + * @param bool $yearsOverflow |
|
316 | + * |
|
317 | + * @return void |
|
318 | + */ |
|
319 | + public static function useYearsOverflow($yearsOverflow = true) |
|
320 | + { |
|
321 | + static::$yearsOverflow = $yearsOverflow; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
326 | + * You should rather use the ->settings() method. |
|
327 | + * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants |
|
328 | + * are available for quarters, years, decade, centuries, millennia (singular and plural forms). |
|
329 | + * @see settings |
|
330 | + * |
|
331 | + * Reset the month overflow behavior. |
|
332 | + * |
|
333 | + * @return void |
|
334 | + */ |
|
335 | + public static function resetYearsOverflow() |
|
336 | + { |
|
337 | + static::$yearsOverflow = true; |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Get the month overflow global behavior (can be overridden in specific instances). |
|
342 | + * |
|
343 | + * @return bool |
|
344 | + */ |
|
345 | + public static function shouldOverflowYears() |
|
346 | + { |
|
347 | + return static::$yearsOverflow; |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * Set specific options. |
|
352 | + * - strictMode: true|false|null |
|
353 | + * - monthOverflow: true|false|null |
|
354 | + * - yearOverflow: true|false|null |
|
355 | + * - humanDiffOptions: int|null |
|
356 | + * - toStringFormat: string|Closure|null |
|
357 | + * - toJsonFormat: string|Closure|null |
|
358 | + * - locale: string|null |
|
359 | + * - timezone: \DateTimeZone|string|int|null |
|
360 | + * - macros: array|null |
|
361 | + * - genericMacros: array|null |
|
362 | + * |
|
363 | + * @param array $settings |
|
364 | + * |
|
365 | + * @return $this|static |
|
366 | + */ |
|
367 | + public function settings(array $settings) |
|
368 | + { |
|
369 | + $this->localStrictModeEnabled = $settings['strictMode'] ?? null; |
|
370 | + $this->localMonthsOverflow = $settings['monthOverflow'] ?? null; |
|
371 | + $this->localYearsOverflow = $settings['yearOverflow'] ?? null; |
|
372 | + $this->localHumanDiffOptions = $settings['humanDiffOptions'] ?? null; |
|
373 | + $this->localToStringFormat = $settings['toStringFormat'] ?? null; |
|
374 | + $this->localSerializer = $settings['toJsonFormat'] ?? null; |
|
375 | + $this->localMacros = $settings['macros'] ?? null; |
|
376 | + $this->localGenericMacros = $settings['genericMacros'] ?? null; |
|
377 | + $this->localFormatFunction = $settings['formatFunction'] ?? null; |
|
378 | + |
|
379 | + if (isset($settings['locale'])) { |
|
380 | + $locales = $settings['locale']; |
|
381 | + |
|
382 | + if (!\is_array($locales)) { |
|
383 | + $locales = [$locales]; |
|
384 | + } |
|
385 | + |
|
386 | + $this->locale(...$locales); |
|
387 | + } |
|
388 | + |
|
389 | + if (isset($settings['innerTimezone'])) { |
|
390 | + return $this->setTimezone($settings['innerTimezone']); |
|
391 | + } |
|
392 | + |
|
393 | + if (isset($settings['timezone'])) { |
|
394 | + return $this->shiftTimezone($settings['timezone']); |
|
395 | + } |
|
396 | + |
|
397 | + return $this; |
|
398 | + } |
|
399 | + |
|
400 | + /** |
|
401 | + * Returns current local settings. |
|
402 | + * |
|
403 | + * @return array |
|
404 | + */ |
|
405 | + public function getSettings() |
|
406 | + { |
|
407 | + $settings = []; |
|
408 | + $map = [ |
|
409 | + 'localStrictModeEnabled' => 'strictMode', |
|
410 | + 'localMonthsOverflow' => 'monthOverflow', |
|
411 | + 'localYearsOverflow' => 'yearOverflow', |
|
412 | + 'localHumanDiffOptions' => 'humanDiffOptions', |
|
413 | + 'localToStringFormat' => 'toStringFormat', |
|
414 | + 'localSerializer' => 'toJsonFormat', |
|
415 | + 'localMacros' => 'macros', |
|
416 | + 'localGenericMacros' => 'genericMacros', |
|
417 | + 'locale' => 'locale', |
|
418 | + 'tzName' => 'timezone', |
|
419 | + 'localFormatFunction' => 'formatFunction', |
|
420 | + ]; |
|
421 | + |
|
422 | + foreach ($map as $property => $key) { |
|
423 | + $value = $this->$property ?? null; |
|
424 | + |
|
425 | + if ($value !== null) { |
|
426 | + $settings[$key] = $value; |
|
427 | + } |
|
428 | + } |
|
429 | + |
|
430 | + return $settings; |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * Show truthy properties on var_dump(). |
|
435 | + * |
|
436 | + * @return array |
|
437 | + */ |
|
438 | + public function __debugInfo() |
|
439 | + { |
|
440 | + $infos = array_filter(get_object_vars($this), function ($var) { |
|
441 | + return $var; |
|
442 | + }); |
|
443 | + |
|
444 | + foreach (['dumpProperties', 'constructedObjectId'] as $property) { |
|
445 | + if (isset($infos[$property])) { |
|
446 | + unset($infos[$property]); |
|
447 | + } |
|
448 | + } |
|
449 | + |
|
450 | + $this->addExtraDebugInfos($infos); |
|
451 | + |
|
452 | + return $infos; |
|
453 | + } |
|
454 | + |
|
455 | + protected function addExtraDebugInfos(&$infos): void |
|
456 | + { |
|
457 | + if ($this instanceof DateTimeInterface) { |
|
458 | + try { |
|
459 | + if (!isset($infos['date'])) { |
|
460 | + $infos['date'] = $this->format(CarbonInterface::MOCK_DATETIME_FORMAT); |
|
461 | + } |
|
462 | + |
|
463 | + if (!isset($infos['timezone'])) { |
|
464 | + $infos['timezone'] = $this->tzName; |
|
465 | + } |
|
466 | + } catch (Throwable $exception) { |
|
467 | + // noop |
|
468 | + } |
|
469 | + } |
|
470 | + } |
|
471 | 471 | } |
@@ -437,7 +437,7 @@ |
||
437 | 437 | */ |
438 | 438 | public function __debugInfo() |
439 | 439 | { |
440 | - $infos = array_filter(get_object_vars($this), function ($var) { |
|
440 | + $infos = array_filter(get_object_vars($this), function($var) { |
|
441 | 441 | return $var; |
442 | 442 | }); |
443 | 443 |
@@ -33,411 +33,411 @@ |
||
33 | 33 | */ |
34 | 34 | trait Boundaries |
35 | 35 | { |
36 | - /** |
|
37 | - * Resets the time to 00:00:00 start of day |
|
38 | - * |
|
39 | - * @example |
|
40 | - * ``` |
|
41 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfDay(); |
|
42 | - * ``` |
|
43 | - * |
|
44 | - * @return static |
|
45 | - */ |
|
46 | - public function startOfDay() |
|
47 | - { |
|
48 | - return $this->setTime(0, 0, 0, 0); |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Resets the time to 23:59:59.999999 end of day |
|
53 | - * |
|
54 | - * @example |
|
55 | - * ``` |
|
56 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfDay(); |
|
57 | - * ``` |
|
58 | - * |
|
59 | - * @return static |
|
60 | - */ |
|
61 | - public function endOfDay() |
|
62 | - { |
|
63 | - return $this->setTime(static::HOURS_PER_DAY - 1, static::MINUTES_PER_HOUR - 1, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Resets the date to the first day of the month and the time to 00:00:00 |
|
68 | - * |
|
69 | - * @example |
|
70 | - * ``` |
|
71 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfMonth(); |
|
72 | - * ``` |
|
73 | - * |
|
74 | - * @return static |
|
75 | - */ |
|
76 | - public function startOfMonth() |
|
77 | - { |
|
78 | - return $this->setDate($this->year, $this->month, 1)->startOfDay(); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Resets the date to end of the month and time to 23:59:59.999999 |
|
83 | - * |
|
84 | - * @example |
|
85 | - * ``` |
|
86 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfMonth(); |
|
87 | - * ``` |
|
88 | - * |
|
89 | - * @return static |
|
90 | - */ |
|
91 | - public function endOfMonth() |
|
92 | - { |
|
93 | - return $this->setDate($this->year, $this->month, $this->daysInMonth)->endOfDay(); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Resets the date to the first day of the quarter and the time to 00:00:00 |
|
98 | - * |
|
99 | - * @example |
|
100 | - * ``` |
|
101 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfQuarter(); |
|
102 | - * ``` |
|
103 | - * |
|
104 | - * @return static |
|
105 | - */ |
|
106 | - public function startOfQuarter() |
|
107 | - { |
|
108 | - $month = ($this->quarter - 1) * static::MONTHS_PER_QUARTER + 1; |
|
109 | - |
|
110 | - return $this->setDate($this->year, $month, 1)->startOfDay(); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Resets the date to end of the quarter and time to 23:59:59.999999 |
|
115 | - * |
|
116 | - * @example |
|
117 | - * ``` |
|
118 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfQuarter(); |
|
119 | - * ``` |
|
120 | - * |
|
121 | - * @return static |
|
122 | - */ |
|
123 | - public function endOfQuarter() |
|
124 | - { |
|
125 | - return $this->startOfQuarter()->addMonths(static::MONTHS_PER_QUARTER - 1)->endOfMonth(); |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * Resets the date to the first day of the year and the time to 00:00:00 |
|
130 | - * |
|
131 | - * @example |
|
132 | - * ``` |
|
133 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfYear(); |
|
134 | - * ``` |
|
135 | - * |
|
136 | - * @return static |
|
137 | - */ |
|
138 | - public function startOfYear() |
|
139 | - { |
|
140 | - return $this->setDate($this->year, 1, 1)->startOfDay(); |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Resets the date to end of the year and time to 23:59:59.999999 |
|
145 | - * |
|
146 | - * @example |
|
147 | - * ``` |
|
148 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfYear(); |
|
149 | - * ``` |
|
150 | - * |
|
151 | - * @return static |
|
152 | - */ |
|
153 | - public function endOfYear() |
|
154 | - { |
|
155 | - return $this->setDate($this->year, 12, 31)->endOfDay(); |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Resets the date to the first day of the decade and the time to 00:00:00 |
|
160 | - * |
|
161 | - * @example |
|
162 | - * ``` |
|
163 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfDecade(); |
|
164 | - * ``` |
|
165 | - * |
|
166 | - * @return static |
|
167 | - */ |
|
168 | - public function startOfDecade() |
|
169 | - { |
|
170 | - $year = $this->year - $this->year % static::YEARS_PER_DECADE; |
|
171 | - |
|
172 | - return $this->setDate($year, 1, 1)->startOfDay(); |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Resets the date to end of the decade and time to 23:59:59.999999 |
|
177 | - * |
|
178 | - * @example |
|
179 | - * ``` |
|
180 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfDecade(); |
|
181 | - * ``` |
|
182 | - * |
|
183 | - * @return static |
|
184 | - */ |
|
185 | - public function endOfDecade() |
|
186 | - { |
|
187 | - $year = $this->year - $this->year % static::YEARS_PER_DECADE + static::YEARS_PER_DECADE - 1; |
|
188 | - |
|
189 | - return $this->setDate($year, 12, 31)->endOfDay(); |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Resets the date to the first day of the century and the time to 00:00:00 |
|
194 | - * |
|
195 | - * @example |
|
196 | - * ``` |
|
197 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfCentury(); |
|
198 | - * ``` |
|
199 | - * |
|
200 | - * @return static |
|
201 | - */ |
|
202 | - public function startOfCentury() |
|
203 | - { |
|
204 | - $year = $this->year - ($this->year - 1) % static::YEARS_PER_CENTURY; |
|
205 | - |
|
206 | - return $this->setDate($year, 1, 1)->startOfDay(); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Resets the date to end of the century and time to 23:59:59.999999 |
|
211 | - * |
|
212 | - * @example |
|
213 | - * ``` |
|
214 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfCentury(); |
|
215 | - * ``` |
|
216 | - * |
|
217 | - * @return static |
|
218 | - */ |
|
219 | - public function endOfCentury() |
|
220 | - { |
|
221 | - $year = $this->year - 1 - ($this->year - 1) % static::YEARS_PER_CENTURY + static::YEARS_PER_CENTURY; |
|
222 | - |
|
223 | - return $this->setDate($year, 12, 31)->endOfDay(); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Resets the date to the first day of the millennium and the time to 00:00:00 |
|
228 | - * |
|
229 | - * @example |
|
230 | - * ``` |
|
231 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfMillennium(); |
|
232 | - * ``` |
|
233 | - * |
|
234 | - * @return static |
|
235 | - */ |
|
236 | - public function startOfMillennium() |
|
237 | - { |
|
238 | - $year = $this->year - ($this->year - 1) % static::YEARS_PER_MILLENNIUM; |
|
239 | - |
|
240 | - return $this->setDate($year, 1, 1)->startOfDay(); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Resets the date to end of the millennium and time to 23:59:59.999999 |
|
245 | - * |
|
246 | - * @example |
|
247 | - * ``` |
|
248 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfMillennium(); |
|
249 | - * ``` |
|
250 | - * |
|
251 | - * @return static |
|
252 | - */ |
|
253 | - public function endOfMillennium() |
|
254 | - { |
|
255 | - $year = $this->year - 1 - ($this->year - 1) % static::YEARS_PER_MILLENNIUM + static::YEARS_PER_MILLENNIUM; |
|
256 | - |
|
257 | - return $this->setDate($year, 12, 31)->endOfDay(); |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00 |
|
262 | - * |
|
263 | - * @example |
|
264 | - * ``` |
|
265 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfWeek() . "\n"; |
|
266 | - * echo Carbon::parse('2018-07-25 12:45:16')->locale('ar')->startOfWeek() . "\n"; |
|
267 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfWeek(Carbon::SUNDAY) . "\n"; |
|
268 | - * ``` |
|
269 | - * |
|
270 | - * @param int $weekStartsAt optional start allow you to specify the day of week to use to start the week |
|
271 | - * |
|
272 | - * @return static |
|
273 | - */ |
|
274 | - public function startOfWeek($weekStartsAt = null) |
|
275 | - { |
|
276 | - return $this->subDays((7 + $this->dayOfWeek - ($weekStartsAt ?? $this->firstWeekDay)) % 7)->startOfDay(); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59.999999 |
|
281 | - * |
|
282 | - * @example |
|
283 | - * ``` |
|
284 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfWeek() . "\n"; |
|
285 | - * echo Carbon::parse('2018-07-25 12:45:16')->locale('ar')->endOfWeek() . "\n"; |
|
286 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfWeek(Carbon::SATURDAY) . "\n"; |
|
287 | - * ``` |
|
288 | - * |
|
289 | - * @param int $weekEndsAt optional start allow you to specify the day of week to use to end the week |
|
290 | - * |
|
291 | - * @return static |
|
292 | - */ |
|
293 | - public function endOfWeek($weekEndsAt = null) |
|
294 | - { |
|
295 | - return $this->addDays((7 - $this->dayOfWeek + ($weekEndsAt ?? $this->lastWeekDay)) % 7)->endOfDay(); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Modify to start of current hour, minutes and seconds become 0 |
|
300 | - * |
|
301 | - * @example |
|
302 | - * ``` |
|
303 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfHour(); |
|
304 | - * ``` |
|
305 | - * |
|
306 | - * @return static |
|
307 | - */ |
|
308 | - public function startOfHour() |
|
309 | - { |
|
310 | - return $this->setTime($this->hour, 0, 0, 0); |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * Modify to end of current hour, minutes and seconds become 59 |
|
315 | - * |
|
316 | - * @example |
|
317 | - * ``` |
|
318 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfHour(); |
|
319 | - * ``` |
|
320 | - * |
|
321 | - * @return static |
|
322 | - */ |
|
323 | - public function endOfHour() |
|
324 | - { |
|
325 | - return $this->setTime($this->hour, static::MINUTES_PER_HOUR - 1, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1); |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Modify to start of current minute, seconds become 0 |
|
330 | - * |
|
331 | - * @example |
|
332 | - * ``` |
|
333 | - * echo Carbon::parse('2018-07-25 12:45:16')->startOfMinute(); |
|
334 | - * ``` |
|
335 | - * |
|
336 | - * @return static |
|
337 | - */ |
|
338 | - public function startOfMinute() |
|
339 | - { |
|
340 | - return $this->setTime($this->hour, $this->minute, 0, 0); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Modify to end of current minute, seconds become 59 |
|
345 | - * |
|
346 | - * @example |
|
347 | - * ``` |
|
348 | - * echo Carbon::parse('2018-07-25 12:45:16')->endOfMinute(); |
|
349 | - * ``` |
|
350 | - * |
|
351 | - * @return static |
|
352 | - */ |
|
353 | - public function endOfMinute() |
|
354 | - { |
|
355 | - return $this->setTime($this->hour, $this->minute, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Modify to start of current second, microseconds become 0 |
|
360 | - * |
|
361 | - * @example |
|
362 | - * ``` |
|
363 | - * echo Carbon::parse('2018-07-25 12:45:16.334455') |
|
364 | - * ->startOfSecond() |
|
365 | - * ->format('H:i:s.u'); |
|
366 | - * ``` |
|
367 | - * |
|
368 | - * @return static |
|
369 | - */ |
|
370 | - public function startOfSecond() |
|
371 | - { |
|
372 | - return $this->setTime($this->hour, $this->minute, $this->second, 0); |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Modify to end of current second, microseconds become 999999 |
|
377 | - * |
|
378 | - * @example |
|
379 | - * ``` |
|
380 | - * echo Carbon::parse('2018-07-25 12:45:16.334455') |
|
381 | - * ->endOfSecond() |
|
382 | - * ->format('H:i:s.u'); |
|
383 | - * ``` |
|
384 | - * |
|
385 | - * @return static |
|
386 | - */ |
|
387 | - public function endOfSecond() |
|
388 | - { |
|
389 | - return $this->setTime($this->hour, $this->minute, $this->second, static::MICROSECONDS_PER_SECOND - 1); |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * Modify to start of current given unit. |
|
394 | - * |
|
395 | - * @example |
|
396 | - * ``` |
|
397 | - * echo Carbon::parse('2018-07-25 12:45:16.334455') |
|
398 | - * ->startOf('month') |
|
399 | - * ->endOf('week', Carbon::FRIDAY); |
|
400 | - * ``` |
|
401 | - * |
|
402 | - * @param string $unit |
|
403 | - * @param array<int, mixed> $params |
|
404 | - * |
|
405 | - * @return static |
|
406 | - */ |
|
407 | - public function startOf($unit, ...$params) |
|
408 | - { |
|
409 | - $ucfUnit = ucfirst(static::singularUnit($unit)); |
|
410 | - $method = "startOf$ucfUnit"; |
|
411 | - if (!method_exists($this, $method)) { |
|
412 | - throw new UnknownUnitException($unit); |
|
413 | - } |
|
414 | - |
|
415 | - return $this->$method(...$params); |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Modify to end of current given unit. |
|
420 | - * |
|
421 | - * @example |
|
422 | - * ``` |
|
423 | - * echo Carbon::parse('2018-07-25 12:45:16.334455') |
|
424 | - * ->startOf('month') |
|
425 | - * ->endOf('week', Carbon::FRIDAY); |
|
426 | - * ``` |
|
427 | - * |
|
428 | - * @param string $unit |
|
429 | - * @param array<int, mixed> $params |
|
430 | - * |
|
431 | - * @return static |
|
432 | - */ |
|
433 | - public function endOf($unit, ...$params) |
|
434 | - { |
|
435 | - $ucfUnit = ucfirst(static::singularUnit($unit)); |
|
436 | - $method = "endOf$ucfUnit"; |
|
437 | - if (!method_exists($this, $method)) { |
|
438 | - throw new UnknownUnitException($unit); |
|
439 | - } |
|
440 | - |
|
441 | - return $this->$method(...$params); |
|
442 | - } |
|
36 | + /** |
|
37 | + * Resets the time to 00:00:00 start of day |
|
38 | + * |
|
39 | + * @example |
|
40 | + * ``` |
|
41 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfDay(); |
|
42 | + * ``` |
|
43 | + * |
|
44 | + * @return static |
|
45 | + */ |
|
46 | + public function startOfDay() |
|
47 | + { |
|
48 | + return $this->setTime(0, 0, 0, 0); |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Resets the time to 23:59:59.999999 end of day |
|
53 | + * |
|
54 | + * @example |
|
55 | + * ``` |
|
56 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfDay(); |
|
57 | + * ``` |
|
58 | + * |
|
59 | + * @return static |
|
60 | + */ |
|
61 | + public function endOfDay() |
|
62 | + { |
|
63 | + return $this->setTime(static::HOURS_PER_DAY - 1, static::MINUTES_PER_HOUR - 1, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Resets the date to the first day of the month and the time to 00:00:00 |
|
68 | + * |
|
69 | + * @example |
|
70 | + * ``` |
|
71 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfMonth(); |
|
72 | + * ``` |
|
73 | + * |
|
74 | + * @return static |
|
75 | + */ |
|
76 | + public function startOfMonth() |
|
77 | + { |
|
78 | + return $this->setDate($this->year, $this->month, 1)->startOfDay(); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Resets the date to end of the month and time to 23:59:59.999999 |
|
83 | + * |
|
84 | + * @example |
|
85 | + * ``` |
|
86 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfMonth(); |
|
87 | + * ``` |
|
88 | + * |
|
89 | + * @return static |
|
90 | + */ |
|
91 | + public function endOfMonth() |
|
92 | + { |
|
93 | + return $this->setDate($this->year, $this->month, $this->daysInMonth)->endOfDay(); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Resets the date to the first day of the quarter and the time to 00:00:00 |
|
98 | + * |
|
99 | + * @example |
|
100 | + * ``` |
|
101 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfQuarter(); |
|
102 | + * ``` |
|
103 | + * |
|
104 | + * @return static |
|
105 | + */ |
|
106 | + public function startOfQuarter() |
|
107 | + { |
|
108 | + $month = ($this->quarter - 1) * static::MONTHS_PER_QUARTER + 1; |
|
109 | + |
|
110 | + return $this->setDate($this->year, $month, 1)->startOfDay(); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Resets the date to end of the quarter and time to 23:59:59.999999 |
|
115 | + * |
|
116 | + * @example |
|
117 | + * ``` |
|
118 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfQuarter(); |
|
119 | + * ``` |
|
120 | + * |
|
121 | + * @return static |
|
122 | + */ |
|
123 | + public function endOfQuarter() |
|
124 | + { |
|
125 | + return $this->startOfQuarter()->addMonths(static::MONTHS_PER_QUARTER - 1)->endOfMonth(); |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * Resets the date to the first day of the year and the time to 00:00:00 |
|
130 | + * |
|
131 | + * @example |
|
132 | + * ``` |
|
133 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfYear(); |
|
134 | + * ``` |
|
135 | + * |
|
136 | + * @return static |
|
137 | + */ |
|
138 | + public function startOfYear() |
|
139 | + { |
|
140 | + return $this->setDate($this->year, 1, 1)->startOfDay(); |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Resets the date to end of the year and time to 23:59:59.999999 |
|
145 | + * |
|
146 | + * @example |
|
147 | + * ``` |
|
148 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfYear(); |
|
149 | + * ``` |
|
150 | + * |
|
151 | + * @return static |
|
152 | + */ |
|
153 | + public function endOfYear() |
|
154 | + { |
|
155 | + return $this->setDate($this->year, 12, 31)->endOfDay(); |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Resets the date to the first day of the decade and the time to 00:00:00 |
|
160 | + * |
|
161 | + * @example |
|
162 | + * ``` |
|
163 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfDecade(); |
|
164 | + * ``` |
|
165 | + * |
|
166 | + * @return static |
|
167 | + */ |
|
168 | + public function startOfDecade() |
|
169 | + { |
|
170 | + $year = $this->year - $this->year % static::YEARS_PER_DECADE; |
|
171 | + |
|
172 | + return $this->setDate($year, 1, 1)->startOfDay(); |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Resets the date to end of the decade and time to 23:59:59.999999 |
|
177 | + * |
|
178 | + * @example |
|
179 | + * ``` |
|
180 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfDecade(); |
|
181 | + * ``` |
|
182 | + * |
|
183 | + * @return static |
|
184 | + */ |
|
185 | + public function endOfDecade() |
|
186 | + { |
|
187 | + $year = $this->year - $this->year % static::YEARS_PER_DECADE + static::YEARS_PER_DECADE - 1; |
|
188 | + |
|
189 | + return $this->setDate($year, 12, 31)->endOfDay(); |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Resets the date to the first day of the century and the time to 00:00:00 |
|
194 | + * |
|
195 | + * @example |
|
196 | + * ``` |
|
197 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfCentury(); |
|
198 | + * ``` |
|
199 | + * |
|
200 | + * @return static |
|
201 | + */ |
|
202 | + public function startOfCentury() |
|
203 | + { |
|
204 | + $year = $this->year - ($this->year - 1) % static::YEARS_PER_CENTURY; |
|
205 | + |
|
206 | + return $this->setDate($year, 1, 1)->startOfDay(); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Resets the date to end of the century and time to 23:59:59.999999 |
|
211 | + * |
|
212 | + * @example |
|
213 | + * ``` |
|
214 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfCentury(); |
|
215 | + * ``` |
|
216 | + * |
|
217 | + * @return static |
|
218 | + */ |
|
219 | + public function endOfCentury() |
|
220 | + { |
|
221 | + $year = $this->year - 1 - ($this->year - 1) % static::YEARS_PER_CENTURY + static::YEARS_PER_CENTURY; |
|
222 | + |
|
223 | + return $this->setDate($year, 12, 31)->endOfDay(); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Resets the date to the first day of the millennium and the time to 00:00:00 |
|
228 | + * |
|
229 | + * @example |
|
230 | + * ``` |
|
231 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfMillennium(); |
|
232 | + * ``` |
|
233 | + * |
|
234 | + * @return static |
|
235 | + */ |
|
236 | + public function startOfMillennium() |
|
237 | + { |
|
238 | + $year = $this->year - ($this->year - 1) % static::YEARS_PER_MILLENNIUM; |
|
239 | + |
|
240 | + return $this->setDate($year, 1, 1)->startOfDay(); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Resets the date to end of the millennium and time to 23:59:59.999999 |
|
245 | + * |
|
246 | + * @example |
|
247 | + * ``` |
|
248 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfMillennium(); |
|
249 | + * ``` |
|
250 | + * |
|
251 | + * @return static |
|
252 | + */ |
|
253 | + public function endOfMillennium() |
|
254 | + { |
|
255 | + $year = $this->year - 1 - ($this->year - 1) % static::YEARS_PER_MILLENNIUM + static::YEARS_PER_MILLENNIUM; |
|
256 | + |
|
257 | + return $this->setDate($year, 12, 31)->endOfDay(); |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Resets the date to the first day of week (defined in $weekStartsAt) and the time to 00:00:00 |
|
262 | + * |
|
263 | + * @example |
|
264 | + * ``` |
|
265 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfWeek() . "\n"; |
|
266 | + * echo Carbon::parse('2018-07-25 12:45:16')->locale('ar')->startOfWeek() . "\n"; |
|
267 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfWeek(Carbon::SUNDAY) . "\n"; |
|
268 | + * ``` |
|
269 | + * |
|
270 | + * @param int $weekStartsAt optional start allow you to specify the day of week to use to start the week |
|
271 | + * |
|
272 | + * @return static |
|
273 | + */ |
|
274 | + public function startOfWeek($weekStartsAt = null) |
|
275 | + { |
|
276 | + return $this->subDays((7 + $this->dayOfWeek - ($weekStartsAt ?? $this->firstWeekDay)) % 7)->startOfDay(); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Resets the date to end of week (defined in $weekEndsAt) and time to 23:59:59.999999 |
|
281 | + * |
|
282 | + * @example |
|
283 | + * ``` |
|
284 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfWeek() . "\n"; |
|
285 | + * echo Carbon::parse('2018-07-25 12:45:16')->locale('ar')->endOfWeek() . "\n"; |
|
286 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfWeek(Carbon::SATURDAY) . "\n"; |
|
287 | + * ``` |
|
288 | + * |
|
289 | + * @param int $weekEndsAt optional start allow you to specify the day of week to use to end the week |
|
290 | + * |
|
291 | + * @return static |
|
292 | + */ |
|
293 | + public function endOfWeek($weekEndsAt = null) |
|
294 | + { |
|
295 | + return $this->addDays((7 - $this->dayOfWeek + ($weekEndsAt ?? $this->lastWeekDay)) % 7)->endOfDay(); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Modify to start of current hour, minutes and seconds become 0 |
|
300 | + * |
|
301 | + * @example |
|
302 | + * ``` |
|
303 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfHour(); |
|
304 | + * ``` |
|
305 | + * |
|
306 | + * @return static |
|
307 | + */ |
|
308 | + public function startOfHour() |
|
309 | + { |
|
310 | + return $this->setTime($this->hour, 0, 0, 0); |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * Modify to end of current hour, minutes and seconds become 59 |
|
315 | + * |
|
316 | + * @example |
|
317 | + * ``` |
|
318 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfHour(); |
|
319 | + * ``` |
|
320 | + * |
|
321 | + * @return static |
|
322 | + */ |
|
323 | + public function endOfHour() |
|
324 | + { |
|
325 | + return $this->setTime($this->hour, static::MINUTES_PER_HOUR - 1, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1); |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Modify to start of current minute, seconds become 0 |
|
330 | + * |
|
331 | + * @example |
|
332 | + * ``` |
|
333 | + * echo Carbon::parse('2018-07-25 12:45:16')->startOfMinute(); |
|
334 | + * ``` |
|
335 | + * |
|
336 | + * @return static |
|
337 | + */ |
|
338 | + public function startOfMinute() |
|
339 | + { |
|
340 | + return $this->setTime($this->hour, $this->minute, 0, 0); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Modify to end of current minute, seconds become 59 |
|
345 | + * |
|
346 | + * @example |
|
347 | + * ``` |
|
348 | + * echo Carbon::parse('2018-07-25 12:45:16')->endOfMinute(); |
|
349 | + * ``` |
|
350 | + * |
|
351 | + * @return static |
|
352 | + */ |
|
353 | + public function endOfMinute() |
|
354 | + { |
|
355 | + return $this->setTime($this->hour, $this->minute, static::SECONDS_PER_MINUTE - 1, static::MICROSECONDS_PER_SECOND - 1); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Modify to start of current second, microseconds become 0 |
|
360 | + * |
|
361 | + * @example |
|
362 | + * ``` |
|
363 | + * echo Carbon::parse('2018-07-25 12:45:16.334455') |
|
364 | + * ->startOfSecond() |
|
365 | + * ->format('H:i:s.u'); |
|
366 | + * ``` |
|
367 | + * |
|
368 | + * @return static |
|
369 | + */ |
|
370 | + public function startOfSecond() |
|
371 | + { |
|
372 | + return $this->setTime($this->hour, $this->minute, $this->second, 0); |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Modify to end of current second, microseconds become 999999 |
|
377 | + * |
|
378 | + * @example |
|
379 | + * ``` |
|
380 | + * echo Carbon::parse('2018-07-25 12:45:16.334455') |
|
381 | + * ->endOfSecond() |
|
382 | + * ->format('H:i:s.u'); |
|
383 | + * ``` |
|
384 | + * |
|
385 | + * @return static |
|
386 | + */ |
|
387 | + public function endOfSecond() |
|
388 | + { |
|
389 | + return $this->setTime($this->hour, $this->minute, $this->second, static::MICROSECONDS_PER_SECOND - 1); |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * Modify to start of current given unit. |
|
394 | + * |
|
395 | + * @example |
|
396 | + * ``` |
|
397 | + * echo Carbon::parse('2018-07-25 12:45:16.334455') |
|
398 | + * ->startOf('month') |
|
399 | + * ->endOf('week', Carbon::FRIDAY); |
|
400 | + * ``` |
|
401 | + * |
|
402 | + * @param string $unit |
|
403 | + * @param array<int, mixed> $params |
|
404 | + * |
|
405 | + * @return static |
|
406 | + */ |
|
407 | + public function startOf($unit, ...$params) |
|
408 | + { |
|
409 | + $ucfUnit = ucfirst(static::singularUnit($unit)); |
|
410 | + $method = "startOf$ucfUnit"; |
|
411 | + if (!method_exists($this, $method)) { |
|
412 | + throw new UnknownUnitException($unit); |
|
413 | + } |
|
414 | + |
|
415 | + return $this->$method(...$params); |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Modify to end of current given unit. |
|
420 | + * |
|
421 | + * @example |
|
422 | + * ``` |
|
423 | + * echo Carbon::parse('2018-07-25 12:45:16.334455') |
|
424 | + * ->startOf('month') |
|
425 | + * ->endOf('week', Carbon::FRIDAY); |
|
426 | + * ``` |
|
427 | + * |
|
428 | + * @param string $unit |
|
429 | + * @param array<int, mixed> $params |
|
430 | + * |
|
431 | + * @return static |
|
432 | + */ |
|
433 | + public function endOf($unit, ...$params) |
|
434 | + { |
|
435 | + $ucfUnit = ucfirst(static::singularUnit($unit)); |
|
436 | + $method = "endOf$ucfUnit"; |
|
437 | + if (!method_exists($this, $method)) { |
|
438 | + throw new UnknownUnitException($unit); |
|
439 | + } |
|
440 | + |
|
441 | + return $this->$method(...$params); |
|
442 | + } |
|
443 | 443 | } |
@@ -407,7 +407,7 @@ discard block |
||
407 | 407 | public function startOf($unit, ...$params) |
408 | 408 | { |
409 | 409 | $ucfUnit = ucfirst(static::singularUnit($unit)); |
410 | - $method = "startOf$ucfUnit"; |
|
410 | + $method = "startof$ucfUnit"; |
|
411 | 411 | if (!method_exists($this, $method)) { |
412 | 412 | throw new UnknownUnitException($unit); |
413 | 413 | } |
@@ -433,7 +433,7 @@ discard block |
||
433 | 433 | public function endOf($unit, ...$params) |
434 | 434 | { |
435 | 435 | $ucfUnit = ucfirst(static::singularUnit($unit)); |
436 | - $method = "endOf$ucfUnit"; |
|
436 | + $method = "endof$ucfUnit"; |
|
437 | 437 | if (!method_exists($this, $method)) { |
438 | 438 | throw new UnknownUnitException($unit); |
439 | 439 | } |
@@ -34,1136 +34,1136 @@ |
||
34 | 34 | */ |
35 | 35 | trait Difference |
36 | 36 | { |
37 | - /** |
|
38 | - * @codeCoverageIgnore |
|
39 | - * |
|
40 | - * @param CarbonInterval $diff |
|
41 | - */ |
|
42 | - protected static function fixNegativeMicroseconds(CarbonInterval $diff) |
|
43 | - { |
|
44 | - if ($diff->s !== 0 || $diff->i !== 0 || $diff->h !== 0 || $diff->d !== 0 || $diff->m !== 0 || $diff->y !== 0) { |
|
45 | - $diff->f = (round($diff->f * 1000000) + 1000000) / 1000000; |
|
46 | - $diff->s--; |
|
47 | - |
|
48 | - if ($diff->s < 0) { |
|
49 | - $diff->s += 60; |
|
50 | - $diff->i--; |
|
51 | - |
|
52 | - if ($diff->i < 0) { |
|
53 | - $diff->i += 60; |
|
54 | - $diff->h--; |
|
55 | - |
|
56 | - if ($diff->h < 0) { |
|
57 | - $diff->h += 24; |
|
58 | - $diff->d--; |
|
59 | - |
|
60 | - if ($diff->d < 0) { |
|
61 | - $diff->d += 30; |
|
62 | - $diff->m--; |
|
63 | - |
|
64 | - if ($diff->m < 0) { |
|
65 | - $diff->m += 12; |
|
66 | - $diff->y--; |
|
67 | - } |
|
68 | - } |
|
69 | - } |
|
70 | - } |
|
71 | - } |
|
72 | - |
|
73 | - return; |
|
74 | - } |
|
75 | - |
|
76 | - $diff->f *= -1; |
|
77 | - $diff->invert(); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @param DateInterval $diff |
|
82 | - * @param bool $absolute |
|
83 | - * |
|
84 | - * @return CarbonInterval |
|
85 | - */ |
|
86 | - protected static function fixDiffInterval(DateInterval $diff, $absolute) |
|
87 | - { |
|
88 | - $diff = CarbonInterval::instance($diff); |
|
89 | - |
|
90 | - // Work-around for https://bugs.php.net/bug.php?id=77145 |
|
91 | - // @codeCoverageIgnoreStart |
|
92 | - if ($diff->f > 0 && $diff->y === -1 && $diff->m === 11 && $diff->d >= 27 && $diff->h === 23 && $diff->i === 59 && $diff->s === 59) { |
|
93 | - $diff->y = 0; |
|
94 | - $diff->m = 0; |
|
95 | - $diff->d = 0; |
|
96 | - $diff->h = 0; |
|
97 | - $diff->i = 0; |
|
98 | - $diff->s = 0; |
|
99 | - $diff->f = (1000000 - round($diff->f * 1000000)) / 1000000; |
|
100 | - $diff->invert(); |
|
101 | - } elseif ($diff->f < 0) { |
|
102 | - static::fixNegativeMicroseconds($diff); |
|
103 | - } |
|
104 | - // @codeCoverageIgnoreEnd |
|
105 | - |
|
106 | - if ($absolute && $diff->invert) { |
|
107 | - $diff->invert(); |
|
108 | - } |
|
109 | - |
|
110 | - return $diff; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get the difference as a DateInterval instance. |
|
115 | - * Return relative interval (negative if $absolute flag is not set to true and the given date is before |
|
116 | - * current one). |
|
117 | - * |
|
118 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
119 | - * @param bool $absolute Get the absolute of the difference |
|
120 | - * |
|
121 | - * @return DateInterval |
|
122 | - */ |
|
123 | - #[ReturnTypeWillChange] |
|
124 | - public function diff($date = null, $absolute = false) |
|
125 | - { |
|
126 | - $other = $this->resolveCarbon($date); |
|
127 | - |
|
128 | - // Work-around for https://bugs.php.net/bug.php?id=81458 |
|
129 | - // It was initially introduced for https://bugs.php.net/bug.php?id=80998 |
|
130 | - // The very specific case of 80998 was fixed in PHP 8.1beta3, but it introduced 81458 |
|
131 | - // So we still need to keep this for now |
|
132 | - // @codeCoverageIgnoreStart |
|
133 | - if (version_compare(PHP_VERSION, '8.1.0-dev', '>=') && $other->tz !== $this->tz) { |
|
134 | - $other = $other->avoidMutation()->tz($this->tz); |
|
135 | - } |
|
136 | - // @codeCoverageIgnoreEnd |
|
137 | - |
|
138 | - return parent::diff($other, (bool) $absolute); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Get the difference as a CarbonInterval instance. |
|
143 | - * Return relative interval (negative if $absolute flag is not set to true and the given date is before |
|
144 | - * current one). |
|
145 | - * |
|
146 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
147 | - * @param bool $absolute Get the absolute of the difference |
|
148 | - * |
|
149 | - * @return CarbonInterval |
|
150 | - */ |
|
151 | - public function diffAsCarbonInterval($date = null, $absolute = true) |
|
152 | - { |
|
153 | - return static::fixDiffInterval($this->diff($this->resolveCarbon($date), $absolute), $absolute); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Get the difference in years |
|
158 | - * |
|
159 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
160 | - * @param bool $absolute Get the absolute of the difference |
|
161 | - * |
|
162 | - * @return int |
|
163 | - */ |
|
164 | - public function diffInYears($date = null, $absolute = true) |
|
165 | - { |
|
166 | - return (int) $this->diff($this->resolveCarbon($date), $absolute)->format('%r%y'); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Get the difference in quarters rounded down. |
|
171 | - * |
|
172 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
173 | - * @param bool $absolute Get the absolute of the difference |
|
174 | - * |
|
175 | - * @return int |
|
176 | - */ |
|
177 | - public function diffInQuarters($date = null, $absolute = true) |
|
178 | - { |
|
179 | - return (int) ($this->diffInMonths($date, $absolute) / static::MONTHS_PER_QUARTER); |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Get the difference in months rounded down. |
|
184 | - * |
|
185 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
186 | - * @param bool $absolute Get the absolute of the difference |
|
187 | - * |
|
188 | - * @return int |
|
189 | - */ |
|
190 | - public function diffInMonths($date = null, $absolute = true) |
|
191 | - { |
|
192 | - $date = $this->resolveCarbon($date); |
|
193 | - |
|
194 | - return $this->diffInYears($date, $absolute) * static::MONTHS_PER_YEAR + (int) $this->diff($date, $absolute)->format('%r%m'); |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Get the difference in weeks rounded down. |
|
199 | - * |
|
200 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
201 | - * @param bool $absolute Get the absolute of the difference |
|
202 | - * |
|
203 | - * @return int |
|
204 | - */ |
|
205 | - public function diffInWeeks($date = null, $absolute = true) |
|
206 | - { |
|
207 | - return (int) ($this->diffInDays($date, $absolute) / static::DAYS_PER_WEEK); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Get the difference in days rounded down. |
|
212 | - * |
|
213 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
214 | - * @param bool $absolute Get the absolute of the difference |
|
215 | - * |
|
216 | - * @return int |
|
217 | - */ |
|
218 | - public function diffInDays($date = null, $absolute = true) |
|
219 | - { |
|
220 | - return $this->getIntervalDayDiff($this->diff($this->resolveCarbon($date), $absolute)); |
|
221 | - } |
|
222 | - |
|
223 | - /** |
|
224 | - * Get the difference in days using a filter closure rounded down. |
|
225 | - * |
|
226 | - * @param Closure $callback |
|
227 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
228 | - * @param bool $absolute Get the absolute of the difference |
|
229 | - * |
|
230 | - * @return int |
|
231 | - */ |
|
232 | - public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = true) |
|
233 | - { |
|
234 | - return $this->diffFiltered(CarbonInterval::day(), $callback, $date, $absolute); |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Get the difference in hours using a filter closure rounded down. |
|
239 | - * |
|
240 | - * @param Closure $callback |
|
241 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
242 | - * @param bool $absolute Get the absolute of the difference |
|
243 | - * |
|
244 | - * @return int |
|
245 | - */ |
|
246 | - public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = true) |
|
247 | - { |
|
248 | - return $this->diffFiltered(CarbonInterval::hour(), $callback, $date, $absolute); |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Get the difference by the given interval using a filter closure. |
|
253 | - * |
|
254 | - * @param CarbonInterval $ci An interval to traverse by |
|
255 | - * @param Closure $callback |
|
256 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
257 | - * @param bool $absolute Get the absolute of the difference |
|
258 | - * |
|
259 | - * @return int |
|
260 | - */ |
|
261 | - public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = true) |
|
262 | - { |
|
263 | - $start = $this; |
|
264 | - $end = $this->resolveCarbon($date); |
|
265 | - $inverse = false; |
|
266 | - |
|
267 | - if ($end < $start) { |
|
268 | - $start = $end; |
|
269 | - $end = $this; |
|
270 | - $inverse = true; |
|
271 | - } |
|
272 | - |
|
273 | - $options = CarbonPeriod::EXCLUDE_END_DATE | ($this->isMutable() ? 0 : CarbonPeriod::IMMUTABLE); |
|
274 | - $diff = $ci->toPeriod($start, $end, $options)->filter($callback)->count(); |
|
275 | - |
|
276 | - return $inverse && !$absolute ? -$diff : $diff; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Get the difference in weekdays rounded down. |
|
281 | - * |
|
282 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
283 | - * @param bool $absolute Get the absolute of the difference |
|
284 | - * |
|
285 | - * @return int |
|
286 | - */ |
|
287 | - public function diffInWeekdays($date = null, $absolute = true) |
|
288 | - { |
|
289 | - return $this->diffInDaysFiltered(function (CarbonInterface $date) { |
|
290 | - return $date->isWeekday(); |
|
291 | - }, $date, $absolute); |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Get the difference in weekend days using a filter rounded down. |
|
296 | - * |
|
297 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
298 | - * @param bool $absolute Get the absolute of the difference |
|
299 | - * |
|
300 | - * @return int |
|
301 | - */ |
|
302 | - public function diffInWeekendDays($date = null, $absolute = true) |
|
303 | - { |
|
304 | - return $this->diffInDaysFiltered(function (CarbonInterface $date) { |
|
305 | - return $date->isWeekend(); |
|
306 | - }, $date, $absolute); |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Get the difference in hours rounded down. |
|
311 | - * |
|
312 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
313 | - * @param bool $absolute Get the absolute of the difference |
|
314 | - * |
|
315 | - * @return int |
|
316 | - */ |
|
317 | - public function diffInHours($date = null, $absolute = true) |
|
318 | - { |
|
319 | - return (int) ($this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR); |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Get the difference in hours rounded down using timestamps. |
|
324 | - * |
|
325 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
326 | - * @param bool $absolute Get the absolute of the difference |
|
327 | - * |
|
328 | - * @return int |
|
329 | - */ |
|
330 | - public function diffInRealHours($date = null, $absolute = true) |
|
331 | - { |
|
332 | - return (int) ($this->diffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR); |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Get the difference in minutes rounded down. |
|
337 | - * |
|
338 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
339 | - * @param bool $absolute Get the absolute of the difference |
|
340 | - * |
|
341 | - * @return int |
|
342 | - */ |
|
343 | - public function diffInMinutes($date = null, $absolute = true) |
|
344 | - { |
|
345 | - return (int) ($this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE); |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * Get the difference in minutes rounded down using timestamps. |
|
350 | - * |
|
351 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
352 | - * @param bool $absolute Get the absolute of the difference |
|
353 | - * |
|
354 | - * @return int |
|
355 | - */ |
|
356 | - public function diffInRealMinutes($date = null, $absolute = true) |
|
357 | - { |
|
358 | - return (int) ($this->diffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE); |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Get the difference in seconds rounded down. |
|
363 | - * |
|
364 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
365 | - * @param bool $absolute Get the absolute of the difference |
|
366 | - * |
|
367 | - * @return int |
|
368 | - */ |
|
369 | - public function diffInSeconds($date = null, $absolute = true) |
|
370 | - { |
|
371 | - $diff = $this->diff($date); |
|
372 | - |
|
373 | - if ($diff->days === 0) { |
|
374 | - $diff = static::fixDiffInterval($diff, $absolute); |
|
375 | - } |
|
376 | - |
|
377 | - $value = (((($diff->m || $diff->y ? $diff->days : $diff->d) * static::HOURS_PER_DAY) + |
|
378 | - $diff->h) * static::MINUTES_PER_HOUR + |
|
379 | - $diff->i) * static::SECONDS_PER_MINUTE + |
|
380 | - $diff->s; |
|
381 | - |
|
382 | - return $absolute || !$diff->invert ? $value : -$value; |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Get the difference in microseconds. |
|
387 | - * |
|
388 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
389 | - * @param bool $absolute Get the absolute of the difference |
|
390 | - * |
|
391 | - * @return int |
|
392 | - */ |
|
393 | - public function diffInMicroseconds($date = null, $absolute = true) |
|
394 | - { |
|
395 | - $diff = $this->diff($date); |
|
396 | - $value = (int) round(((((($diff->m || $diff->y ? $diff->days : $diff->d) * static::HOURS_PER_DAY) + |
|
397 | - $diff->h) * static::MINUTES_PER_HOUR + |
|
398 | - $diff->i) * static::SECONDS_PER_MINUTE + |
|
399 | - ($diff->f + $diff->s)) * static::MICROSECONDS_PER_SECOND); |
|
400 | - |
|
401 | - return $absolute || !$diff->invert ? $value : -$value; |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * Get the difference in milliseconds rounded down. |
|
406 | - * |
|
407 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
408 | - * @param bool $absolute Get the absolute of the difference |
|
409 | - * |
|
410 | - * @return int |
|
411 | - */ |
|
412 | - public function diffInMilliseconds($date = null, $absolute = true) |
|
413 | - { |
|
414 | - return (int) ($this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND); |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Get the difference in seconds using timestamps. |
|
419 | - * |
|
420 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
421 | - * @param bool $absolute Get the absolute of the difference |
|
422 | - * |
|
423 | - * @return int |
|
424 | - */ |
|
425 | - public function diffInRealSeconds($date = null, $absolute = true) |
|
426 | - { |
|
427 | - /** @var CarbonInterface $date */ |
|
428 | - $date = $this->resolveCarbon($date); |
|
429 | - $value = $date->getTimestamp() - $this->getTimestamp(); |
|
430 | - |
|
431 | - return $absolute ? abs($value) : $value; |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Get the difference in microseconds using timestamps. |
|
436 | - * |
|
437 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
438 | - * @param bool $absolute Get the absolute of the difference |
|
439 | - * |
|
440 | - * @return int |
|
441 | - */ |
|
442 | - public function diffInRealMicroseconds($date = null, $absolute = true) |
|
443 | - { |
|
444 | - /** @var CarbonInterface $date */ |
|
445 | - $date = $this->resolveCarbon($date); |
|
446 | - $value = ($date->timestamp - $this->timestamp) * static::MICROSECONDS_PER_SECOND + |
|
447 | - $date->micro - $this->micro; |
|
448 | - |
|
449 | - return $absolute ? abs($value) : $value; |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Get the difference in milliseconds rounded down using timestamps. |
|
454 | - * |
|
455 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
456 | - * @param bool $absolute Get the absolute of the difference |
|
457 | - * |
|
458 | - * @return int |
|
459 | - */ |
|
460 | - public function diffInRealMilliseconds($date = null, $absolute = true) |
|
461 | - { |
|
462 | - return (int) ($this->diffInRealMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND); |
|
463 | - } |
|
464 | - |
|
465 | - /** |
|
466 | - * Get the difference in seconds as float (microsecond-precision). |
|
467 | - * |
|
468 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
469 | - * @param bool $absolute Get the absolute of the difference |
|
470 | - * |
|
471 | - * @return float |
|
472 | - */ |
|
473 | - public function floatDiffInSeconds($date = null, $absolute = true) |
|
474 | - { |
|
475 | - return (float) ($this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_SECOND); |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * Get the difference in minutes as float (microsecond-precision). |
|
480 | - * |
|
481 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
482 | - * @param bool $absolute Get the absolute of the difference |
|
483 | - * |
|
484 | - * @return float |
|
485 | - */ |
|
486 | - public function floatDiffInMinutes($date = null, $absolute = true) |
|
487 | - { |
|
488 | - return $this->floatDiffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; |
|
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * Get the difference in hours as float (microsecond-precision). |
|
493 | - * |
|
494 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
495 | - * @param bool $absolute Get the absolute of the difference |
|
496 | - * |
|
497 | - * @return float |
|
498 | - */ |
|
499 | - public function floatDiffInHours($date = null, $absolute = true) |
|
500 | - { |
|
501 | - return $this->floatDiffInMinutes($date, $absolute) / static::MINUTES_PER_HOUR; |
|
502 | - } |
|
503 | - |
|
504 | - /** |
|
505 | - * Get the difference in days as float (microsecond-precision). |
|
506 | - * |
|
507 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
508 | - * @param bool $absolute Get the absolute of the difference |
|
509 | - * |
|
510 | - * @return float |
|
511 | - */ |
|
512 | - public function floatDiffInDays($date = null, $absolute = true) |
|
513 | - { |
|
514 | - $hoursDiff = $this->floatDiffInHours($date, $absolute); |
|
515 | - $interval = $this->diff($date, $absolute); |
|
516 | - |
|
517 | - if ($interval->y === 0 && $interval->m === 0 && $interval->d === 0) { |
|
518 | - return $hoursDiff / static::HOURS_PER_DAY; |
|
519 | - } |
|
520 | - |
|
521 | - $daysDiff = $this->getIntervalDayDiff($interval); |
|
522 | - |
|
523 | - return $daysDiff + fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; |
|
524 | - } |
|
525 | - |
|
526 | - /** |
|
527 | - * Get the difference in weeks as float (microsecond-precision). |
|
528 | - * |
|
529 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
530 | - * @param bool $absolute Get the absolute of the difference |
|
531 | - * |
|
532 | - * @return float |
|
533 | - */ |
|
534 | - public function floatDiffInWeeks($date = null, $absolute = true) |
|
535 | - { |
|
536 | - return $this->floatDiffInDays($date, $absolute) / static::DAYS_PER_WEEK; |
|
537 | - } |
|
538 | - |
|
539 | - /** |
|
540 | - * Get the difference in months as float (microsecond-precision). |
|
541 | - * |
|
542 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
543 | - * @param bool $absolute Get the absolute of the difference |
|
544 | - * |
|
545 | - * @return float |
|
546 | - */ |
|
547 | - public function floatDiffInMonths($date = null, $absolute = true) |
|
548 | - { |
|
549 | - $start = $this; |
|
550 | - $end = $this->resolveCarbon($date); |
|
551 | - $ascending = ($start <= $end); |
|
552 | - $sign = $absolute || $ascending ? 1 : -1; |
|
553 | - if (!$ascending) { |
|
554 | - [$start, $end] = [$end, $start]; |
|
555 | - } |
|
556 | - $monthsDiff = $start->diffInMonths($end); |
|
557 | - /** @var Carbon|CarbonImmutable $floorEnd */ |
|
558 | - $floorEnd = $start->avoidMutation()->addMonths($monthsDiff); |
|
559 | - |
|
560 | - if ($floorEnd >= $end) { |
|
561 | - return $sign * $monthsDiff; |
|
562 | - } |
|
563 | - |
|
564 | - /** @var Carbon|CarbonImmutable $startOfMonthAfterFloorEnd */ |
|
565 | - $startOfMonthAfterFloorEnd = $floorEnd->avoidMutation()->addMonth()->startOfMonth(); |
|
566 | - |
|
567 | - if ($startOfMonthAfterFloorEnd > $end) { |
|
568 | - return $sign * ($monthsDiff + $floorEnd->floatDiffInDays($end) / $floorEnd->daysInMonth); |
|
569 | - } |
|
570 | - |
|
571 | - return $sign * ($monthsDiff + $floorEnd->floatDiffInDays($startOfMonthAfterFloorEnd) / $floorEnd->daysInMonth + $startOfMonthAfterFloorEnd->floatDiffInDays($end) / $end->daysInMonth); |
|
572 | - } |
|
573 | - |
|
574 | - /** |
|
575 | - * Get the difference in year as float (microsecond-precision). |
|
576 | - * |
|
577 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
578 | - * @param bool $absolute Get the absolute of the difference |
|
579 | - * |
|
580 | - * @return float |
|
581 | - */ |
|
582 | - public function floatDiffInYears($date = null, $absolute = true) |
|
583 | - { |
|
584 | - $start = $this; |
|
585 | - $end = $this->resolveCarbon($date); |
|
586 | - $ascending = ($start <= $end); |
|
587 | - $sign = $absolute || $ascending ? 1 : -1; |
|
588 | - if (!$ascending) { |
|
589 | - [$start, $end] = [$end, $start]; |
|
590 | - } |
|
591 | - $yearsDiff = $start->diffInYears($end); |
|
592 | - /** @var Carbon|CarbonImmutable $floorEnd */ |
|
593 | - $floorEnd = $start->avoidMutation()->addYears($yearsDiff); |
|
594 | - |
|
595 | - if ($floorEnd >= $end) { |
|
596 | - return $sign * $yearsDiff; |
|
597 | - } |
|
598 | - |
|
599 | - /** @var Carbon|CarbonImmutable $startOfYearAfterFloorEnd */ |
|
600 | - $startOfYearAfterFloorEnd = $floorEnd->avoidMutation()->addYear()->startOfYear(); |
|
601 | - |
|
602 | - if ($startOfYearAfterFloorEnd > $end) { |
|
603 | - return $sign * ($yearsDiff + $floorEnd->floatDiffInDays($end) / $floorEnd->daysInYear); |
|
604 | - } |
|
605 | - |
|
606 | - return $sign * ($yearsDiff + $floorEnd->floatDiffInDays($startOfYearAfterFloorEnd) / $floorEnd->daysInYear + $startOfYearAfterFloorEnd->floatDiffInDays($end) / $end->daysInYear); |
|
607 | - } |
|
608 | - |
|
609 | - /** |
|
610 | - * Get the difference in seconds as float (microsecond-precision) using timestamps. |
|
611 | - * |
|
612 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
613 | - * @param bool $absolute Get the absolute of the difference |
|
614 | - * |
|
615 | - * @return float |
|
616 | - */ |
|
617 | - public function floatDiffInRealSeconds($date = null, $absolute = true) |
|
618 | - { |
|
619 | - return $this->diffInRealMicroseconds($date, $absolute) / static::MICROSECONDS_PER_SECOND; |
|
620 | - } |
|
621 | - |
|
622 | - /** |
|
623 | - * Get the difference in minutes as float (microsecond-precision) using timestamps. |
|
624 | - * |
|
625 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
626 | - * @param bool $absolute Get the absolute of the difference |
|
627 | - * |
|
628 | - * @return float |
|
629 | - */ |
|
630 | - public function floatDiffInRealMinutes($date = null, $absolute = true) |
|
631 | - { |
|
632 | - return $this->floatDiffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; |
|
633 | - } |
|
634 | - |
|
635 | - /** |
|
636 | - * Get the difference in hours as float (microsecond-precision) using timestamps. |
|
637 | - * |
|
638 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
639 | - * @param bool $absolute Get the absolute of the difference |
|
640 | - * |
|
641 | - * @return float |
|
642 | - */ |
|
643 | - public function floatDiffInRealHours($date = null, $absolute = true) |
|
644 | - { |
|
645 | - return $this->floatDiffInRealMinutes($date, $absolute) / static::MINUTES_PER_HOUR; |
|
646 | - } |
|
647 | - |
|
648 | - /** |
|
649 | - * Get the difference in days as float (microsecond-precision). |
|
650 | - * |
|
651 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
652 | - * @param bool $absolute Get the absolute of the difference |
|
653 | - * |
|
654 | - * @return float |
|
655 | - */ |
|
656 | - public function floatDiffInRealDays($date = null, $absolute = true) |
|
657 | - { |
|
658 | - $date = $this->resolveUTC($date); |
|
659 | - $utc = $this->avoidMutation()->utc(); |
|
660 | - $hoursDiff = $utc->floatDiffInRealHours($date, $absolute); |
|
661 | - |
|
662 | - return ($hoursDiff < 0 ? -1 : 1) * $utc->diffInDays($date) + fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; |
|
663 | - } |
|
664 | - |
|
665 | - /** |
|
666 | - * Get the difference in weeks as float (microsecond-precision). |
|
667 | - * |
|
668 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
669 | - * @param bool $absolute Get the absolute of the difference |
|
670 | - * |
|
671 | - * @return float |
|
672 | - */ |
|
673 | - public function floatDiffInRealWeeks($date = null, $absolute = true) |
|
674 | - { |
|
675 | - return $this->floatDiffInRealDays($date, $absolute) / static::DAYS_PER_WEEK; |
|
676 | - } |
|
677 | - |
|
678 | - /** |
|
679 | - * Get the difference in months as float (microsecond-precision) using timestamps. |
|
680 | - * |
|
681 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
682 | - * @param bool $absolute Get the absolute of the difference |
|
683 | - * |
|
684 | - * @return float |
|
685 | - */ |
|
686 | - public function floatDiffInRealMonths($date = null, $absolute = true) |
|
687 | - { |
|
688 | - $start = $this; |
|
689 | - $end = $this->resolveCarbon($date); |
|
690 | - $ascending = ($start <= $end); |
|
691 | - $sign = $absolute || $ascending ? 1 : -1; |
|
692 | - if (!$ascending) { |
|
693 | - [$start, $end] = [$end, $start]; |
|
694 | - } |
|
695 | - $monthsDiff = $start->diffInMonths($end); |
|
696 | - /** @var Carbon|CarbonImmutable $floorEnd */ |
|
697 | - $floorEnd = $start->avoidMutation()->addMonths($monthsDiff); |
|
698 | - |
|
699 | - if ($floorEnd >= $end) { |
|
700 | - return $sign * $monthsDiff; |
|
701 | - } |
|
702 | - |
|
703 | - /** @var Carbon|CarbonImmutable $startOfMonthAfterFloorEnd */ |
|
704 | - $startOfMonthAfterFloorEnd = $floorEnd->avoidMutation()->addMonth()->startOfMonth(); |
|
705 | - |
|
706 | - if ($startOfMonthAfterFloorEnd > $end) { |
|
707 | - return $sign * ($monthsDiff + $floorEnd->floatDiffInRealDays($end) / $floorEnd->daysInMonth); |
|
708 | - } |
|
709 | - |
|
710 | - return $sign * ($monthsDiff + $floorEnd->floatDiffInRealDays($startOfMonthAfterFloorEnd) / $floorEnd->daysInMonth + $startOfMonthAfterFloorEnd->floatDiffInRealDays($end) / $end->daysInMonth); |
|
711 | - } |
|
712 | - |
|
713 | - /** |
|
714 | - * Get the difference in year as float (microsecond-precision) using timestamps. |
|
715 | - * |
|
716 | - * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
717 | - * @param bool $absolute Get the absolute of the difference |
|
718 | - * |
|
719 | - * @return float |
|
720 | - */ |
|
721 | - public function floatDiffInRealYears($date = null, $absolute = true) |
|
722 | - { |
|
723 | - $start = $this; |
|
724 | - $end = $this->resolveCarbon($date); |
|
725 | - $ascending = ($start <= $end); |
|
726 | - $sign = $absolute || $ascending ? 1 : -1; |
|
727 | - if (!$ascending) { |
|
728 | - [$start, $end] = [$end, $start]; |
|
729 | - } |
|
730 | - $yearsDiff = $start->diffInYears($end); |
|
731 | - /** @var Carbon|CarbonImmutable $floorEnd */ |
|
732 | - $floorEnd = $start->avoidMutation()->addYears($yearsDiff); |
|
733 | - |
|
734 | - if ($floorEnd >= $end) { |
|
735 | - return $sign * $yearsDiff; |
|
736 | - } |
|
737 | - |
|
738 | - /** @var Carbon|CarbonImmutable $startOfYearAfterFloorEnd */ |
|
739 | - $startOfYearAfterFloorEnd = $floorEnd->avoidMutation()->addYear()->startOfYear(); |
|
740 | - |
|
741 | - if ($startOfYearAfterFloorEnd > $end) { |
|
742 | - return $sign * ($yearsDiff + $floorEnd->floatDiffInRealDays($end) / $floorEnd->daysInYear); |
|
743 | - } |
|
744 | - |
|
745 | - return $sign * ($yearsDiff + $floorEnd->floatDiffInRealDays($startOfYearAfterFloorEnd) / $floorEnd->daysInYear + $startOfYearAfterFloorEnd->floatDiffInRealDays($end) / $end->daysInYear); |
|
746 | - } |
|
747 | - |
|
748 | - /** |
|
749 | - * The number of seconds since midnight. |
|
750 | - * |
|
751 | - * @return int |
|
752 | - */ |
|
753 | - public function secondsSinceMidnight() |
|
754 | - { |
|
755 | - return $this->diffInSeconds($this->avoidMutation()->startOfDay()); |
|
756 | - } |
|
757 | - |
|
758 | - /** |
|
759 | - * The number of seconds until 23:59:59. |
|
760 | - * |
|
761 | - * @return int |
|
762 | - */ |
|
763 | - public function secondsUntilEndOfDay() |
|
764 | - { |
|
765 | - return $this->diffInSeconds($this->avoidMutation()->endOfDay()); |
|
766 | - } |
|
767 | - |
|
768 | - /** |
|
769 | - * Get the difference in a human readable format in the current locale from current instance to an other |
|
770 | - * instance given (or now if null given). |
|
771 | - * |
|
772 | - * @example |
|
773 | - * ``` |
|
774 | - * echo Carbon::tomorrow()->diffForHumans() . "\n"; |
|
775 | - * echo Carbon::tomorrow()->diffForHumans(['parts' => 2]) . "\n"; |
|
776 | - * echo Carbon::tomorrow()->diffForHumans(['parts' => 3, 'join' => true]) . "\n"; |
|
777 | - * echo Carbon::tomorrow()->diffForHumans(Carbon::yesterday()) . "\n"; |
|
778 | - * echo Carbon::tomorrow()->diffForHumans(Carbon::yesterday(), ['short' => true]) . "\n"; |
|
779 | - * ``` |
|
780 | - * |
|
781 | - * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below; |
|
782 | - * if null passed, now will be used as comparison reference; |
|
783 | - * if any other type, it will be converted to date and used as reference. |
|
784 | - * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
785 | - * - 'syntax' entry (see below) |
|
786 | - * - 'short' entry (see below) |
|
787 | - * - 'parts' entry (see below) |
|
788 | - * - 'options' entry (see below) |
|
789 | - * - 'skip' entry, list of units to skip (array of strings or a single string, |
|
790 | - * ` it can be the unit name (singular or plural) or its shortcut |
|
791 | - * ` (y, m, w, d, h, min, s, ms, µs). |
|
792 | - * - 'aUnit' entry, prefer "an hour" over "1 hour" if true |
|
793 | - * - 'join' entry determines how to join multiple parts of the string |
|
794 | - * ` - if $join is a string, it's used as a joiner glue |
|
795 | - * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
796 | - * ` - if $join is an array, the first item will be the default glue, and the second item |
|
797 | - * ` will be used instead of the glue for the last item |
|
798 | - * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
799 | - * ` - if $join is missing, a space will be used as glue |
|
800 | - * - 'other' entry (see above) |
|
801 | - * - 'minimumUnit' entry determines the smallest unit of time to display can be long or |
|
802 | - * ` short form of the units, e.g. 'hour' or 'h' (default value: s) |
|
803 | - * if int passed, it add modifiers: |
|
804 | - * Possible values: |
|
805 | - * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
806 | - * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
807 | - * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
808 | - * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
809 | - * @param bool $short displays short format of time units |
|
810 | - * @param int $parts maximum number of parts to display (default value: 1: single unit) |
|
811 | - * @param int $options human diff options |
|
812 | - * |
|
813 | - * @return string |
|
814 | - */ |
|
815 | - public function diffForHumans($other = null, $syntax = null, $short = false, $parts = 1, $options = null) |
|
816 | - { |
|
817 | - /* @var CarbonInterface $this */ |
|
818 | - if (\is_array($other)) { |
|
819 | - $other['syntax'] = \array_key_exists('syntax', $other) ? $other['syntax'] : $syntax; |
|
820 | - $syntax = $other; |
|
821 | - $other = $syntax['other'] ?? null; |
|
822 | - } |
|
823 | - |
|
824 | - $intSyntax = &$syntax; |
|
825 | - if (\is_array($syntax)) { |
|
826 | - $syntax['syntax'] = $syntax['syntax'] ?? null; |
|
827 | - $intSyntax = &$syntax['syntax']; |
|
828 | - } |
|
829 | - $intSyntax = (int) ($intSyntax ?? static::DIFF_RELATIVE_AUTO); |
|
830 | - $intSyntax = $intSyntax === static::DIFF_RELATIVE_AUTO && $other === null ? static::DIFF_RELATIVE_TO_NOW : $intSyntax; |
|
831 | - |
|
832 | - $parts = min(7, max(1, (int) $parts)); |
|
833 | - |
|
834 | - return $this->diffAsCarbonInterval($other, false) |
|
835 | - ->setLocalTranslator($this->getLocalTranslator()) |
|
836 | - ->forHumans($syntax, (bool) $short, $parts, $options ?? $this->localHumanDiffOptions ?? static::getHumanDiffOptions()); |
|
837 | - } |
|
838 | - |
|
839 | - /** |
|
840 | - * @alias diffForHumans |
|
841 | - * |
|
842 | - * Get the difference in a human readable format in the current locale from current instance to an other |
|
843 | - * instance given (or now if null given). |
|
844 | - * |
|
845 | - * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below; |
|
846 | - * if null passed, now will be used as comparison reference; |
|
847 | - * if any other type, it will be converted to date and used as reference. |
|
848 | - * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
849 | - * - 'syntax' entry (see below) |
|
850 | - * - 'short' entry (see below) |
|
851 | - * - 'parts' entry (see below) |
|
852 | - * - 'options' entry (see below) |
|
853 | - * - 'join' entry determines how to join multiple parts of the string |
|
854 | - * ` - if $join is a string, it's used as a joiner glue |
|
855 | - * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
856 | - * ` - if $join is an array, the first item will be the default glue, and the second item |
|
857 | - * ` will be used instead of the glue for the last item |
|
858 | - * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
859 | - * ` - if $join is missing, a space will be used as glue |
|
860 | - * - 'other' entry (see above) |
|
861 | - * if int passed, it add modifiers: |
|
862 | - * Possible values: |
|
863 | - * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
864 | - * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
865 | - * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
866 | - * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
867 | - * @param bool $short displays short format of time units |
|
868 | - * @param int $parts maximum number of parts to display (default value: 1: single unit) |
|
869 | - * @param int $options human diff options |
|
870 | - * |
|
871 | - * @return string |
|
872 | - */ |
|
873 | - public function from($other = null, $syntax = null, $short = false, $parts = 1, $options = null) |
|
874 | - { |
|
875 | - return $this->diffForHumans($other, $syntax, $short, $parts, $options); |
|
876 | - } |
|
877 | - |
|
878 | - /** |
|
879 | - * @alias diffForHumans |
|
880 | - * |
|
881 | - * Get the difference in a human readable format in the current locale from current instance to an other |
|
882 | - * instance given (or now if null given). |
|
883 | - */ |
|
884 | - public function since($other = null, $syntax = null, $short = false, $parts = 1, $options = null) |
|
885 | - { |
|
886 | - return $this->diffForHumans($other, $syntax, $short, $parts, $options); |
|
887 | - } |
|
888 | - |
|
889 | - /** |
|
890 | - * Get the difference in a human readable format in the current locale from an other |
|
891 | - * instance given (or now if null given) to current instance. |
|
892 | - * |
|
893 | - * When comparing a value in the past to default now: |
|
894 | - * 1 hour from now |
|
895 | - * 5 months from now |
|
896 | - * |
|
897 | - * When comparing a value in the future to default now: |
|
898 | - * 1 hour ago |
|
899 | - * 5 months ago |
|
900 | - * |
|
901 | - * When comparing a value in the past to another value: |
|
902 | - * 1 hour after |
|
903 | - * 5 months after |
|
904 | - * |
|
905 | - * When comparing a value in the future to another value: |
|
906 | - * 1 hour before |
|
907 | - * 5 months before |
|
908 | - * |
|
909 | - * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below; |
|
910 | - * if null passed, now will be used as comparison reference; |
|
911 | - * if any other type, it will be converted to date and used as reference. |
|
912 | - * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
913 | - * - 'syntax' entry (see below) |
|
914 | - * - 'short' entry (see below) |
|
915 | - * - 'parts' entry (see below) |
|
916 | - * - 'options' entry (see below) |
|
917 | - * - 'join' entry determines how to join multiple parts of the string |
|
918 | - * ` - if $join is a string, it's used as a joiner glue |
|
919 | - * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
920 | - * ` - if $join is an array, the first item will be the default glue, and the second item |
|
921 | - * ` will be used instead of the glue for the last item |
|
922 | - * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
923 | - * ` - if $join is missing, a space will be used as glue |
|
924 | - * - 'other' entry (see above) |
|
925 | - * if int passed, it add modifiers: |
|
926 | - * Possible values: |
|
927 | - * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
928 | - * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
929 | - * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
930 | - * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
931 | - * @param bool $short displays short format of time units |
|
932 | - * @param int $parts maximum number of parts to display (default value: 1: single unit) |
|
933 | - * @param int $options human diff options |
|
934 | - * |
|
935 | - * @return string |
|
936 | - */ |
|
937 | - public function to($other = null, $syntax = null, $short = false, $parts = 1, $options = null) |
|
938 | - { |
|
939 | - if (!$syntax && !$other) { |
|
940 | - $syntax = CarbonInterface::DIFF_RELATIVE_TO_NOW; |
|
941 | - } |
|
942 | - |
|
943 | - return $this->resolveCarbon($other)->diffForHumans($this, $syntax, $short, $parts, $options); |
|
944 | - } |
|
945 | - |
|
946 | - /** |
|
947 | - * @alias to |
|
948 | - * |
|
949 | - * Get the difference in a human readable format in the current locale from an other |
|
950 | - * instance given (or now if null given) to current instance. |
|
951 | - * |
|
952 | - * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below; |
|
953 | - * if null passed, now will be used as comparison reference; |
|
954 | - * if any other type, it will be converted to date and used as reference. |
|
955 | - * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
956 | - * - 'syntax' entry (see below) |
|
957 | - * - 'short' entry (see below) |
|
958 | - * - 'parts' entry (see below) |
|
959 | - * - 'options' entry (see below) |
|
960 | - * - 'join' entry determines how to join multiple parts of the string |
|
961 | - * ` - if $join is a string, it's used as a joiner glue |
|
962 | - * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
963 | - * ` - if $join is an array, the first item will be the default glue, and the second item |
|
964 | - * ` will be used instead of the glue for the last item |
|
965 | - * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
966 | - * ` - if $join is missing, a space will be used as glue |
|
967 | - * - 'other' entry (see above) |
|
968 | - * if int passed, it add modifiers: |
|
969 | - * Possible values: |
|
970 | - * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
971 | - * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
972 | - * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
973 | - * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
974 | - * @param bool $short displays short format of time units |
|
975 | - * @param int $parts maximum number of parts to display (default value: 1: single unit) |
|
976 | - * @param int $options human diff options |
|
977 | - * |
|
978 | - * @return string |
|
979 | - */ |
|
980 | - public function until($other = null, $syntax = null, $short = false, $parts = 1, $options = null) |
|
981 | - { |
|
982 | - return $this->to($other, $syntax, $short, $parts, $options); |
|
983 | - } |
|
984 | - |
|
985 | - /** |
|
986 | - * Get the difference in a human readable format in the current locale from current |
|
987 | - * instance to now. |
|
988 | - * |
|
989 | - * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
990 | - * - 'syntax' entry (see below) |
|
991 | - * - 'short' entry (see below) |
|
992 | - * - 'parts' entry (see below) |
|
993 | - * - 'options' entry (see below) |
|
994 | - * - 'join' entry determines how to join multiple parts of the string |
|
995 | - * ` - if $join is a string, it's used as a joiner glue |
|
996 | - * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
997 | - * ` - if $join is an array, the first item will be the default glue, and the second item |
|
998 | - * ` will be used instead of the glue for the last item |
|
999 | - * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
1000 | - * ` - if $join is missing, a space will be used as glue |
|
1001 | - * if int passed, it add modifiers: |
|
1002 | - * Possible values: |
|
1003 | - * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
1004 | - * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
1005 | - * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
1006 | - * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
1007 | - * @param bool $short displays short format of time units |
|
1008 | - * @param int $parts maximum number of parts to display (default value: 1: single unit) |
|
1009 | - * @param int $options human diff options |
|
1010 | - * |
|
1011 | - * @return string |
|
1012 | - */ |
|
1013 | - public function fromNow($syntax = null, $short = false, $parts = 1, $options = null) |
|
1014 | - { |
|
1015 | - $other = null; |
|
1016 | - |
|
1017 | - if ($syntax instanceof DateTimeInterface) { |
|
1018 | - [$other, $syntax, $short, $parts, $options] = array_pad(\func_get_args(), 5, null); |
|
1019 | - } |
|
1020 | - |
|
1021 | - return $this->from($other, $syntax, $short, $parts, $options); |
|
1022 | - } |
|
1023 | - |
|
1024 | - /** |
|
1025 | - * Get the difference in a human readable format in the current locale from an other |
|
1026 | - * instance given to now |
|
1027 | - * |
|
1028 | - * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
1029 | - * - 'syntax' entry (see below) |
|
1030 | - * - 'short' entry (see below) |
|
1031 | - * - 'parts' entry (see below) |
|
1032 | - * - 'options' entry (see below) |
|
1033 | - * - 'join' entry determines how to join multiple parts of the string |
|
1034 | - * ` - if $join is a string, it's used as a joiner glue |
|
1035 | - * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
1036 | - * ` - if $join is an array, the first item will be the default glue, and the second item |
|
1037 | - * ` will be used instead of the glue for the last item |
|
1038 | - * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
1039 | - * ` - if $join is missing, a space will be used as glue |
|
1040 | - * if int passed, it add modifiers: |
|
1041 | - * Possible values: |
|
1042 | - * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
1043 | - * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
1044 | - * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
1045 | - * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
1046 | - * @param bool $short displays short format of time units |
|
1047 | - * @param int $parts maximum number of parts to display (default value: 1: single part) |
|
1048 | - * @param int $options human diff options |
|
1049 | - * |
|
1050 | - * @return string |
|
1051 | - */ |
|
1052 | - public function toNow($syntax = null, $short = false, $parts = 1, $options = null) |
|
1053 | - { |
|
1054 | - return $this->to(null, $syntax, $short, $parts, $options); |
|
1055 | - } |
|
1056 | - |
|
1057 | - /** |
|
1058 | - * Get the difference in a human readable format in the current locale from an other |
|
1059 | - * instance given to now |
|
1060 | - * |
|
1061 | - * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
1062 | - * - 'syntax' entry (see below) |
|
1063 | - * - 'short' entry (see below) |
|
1064 | - * - 'parts' entry (see below) |
|
1065 | - * - 'options' entry (see below) |
|
1066 | - * - 'join' entry determines how to join multiple parts of the string |
|
1067 | - * ` - if $join is a string, it's used as a joiner glue |
|
1068 | - * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
1069 | - * ` - if $join is an array, the first item will be the default glue, and the second item |
|
1070 | - * ` will be used instead of the glue for the last item |
|
1071 | - * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
1072 | - * ` - if $join is missing, a space will be used as glue |
|
1073 | - * if int passed, it add modifiers: |
|
1074 | - * Possible values: |
|
1075 | - * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
1076 | - * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
1077 | - * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
1078 | - * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
1079 | - * @param bool $short displays short format of time units |
|
1080 | - * @param int $parts maximum number of parts to display (default value: 1: single part) |
|
1081 | - * @param int $options human diff options |
|
1082 | - * |
|
1083 | - * @return string |
|
1084 | - */ |
|
1085 | - public function ago($syntax = null, $short = false, $parts = 1, $options = null) |
|
1086 | - { |
|
1087 | - $other = null; |
|
1088 | - |
|
1089 | - if ($syntax instanceof DateTimeInterface) { |
|
1090 | - [$other, $syntax, $short, $parts, $options] = array_pad(\func_get_args(), 5, null); |
|
1091 | - } |
|
1092 | - |
|
1093 | - return $this->from($other, $syntax, $short, $parts, $options); |
|
1094 | - } |
|
1095 | - |
|
1096 | - /** |
|
1097 | - * Get the difference in a human readable format in the current locale from current instance to an other |
|
1098 | - * instance given (or now if null given). |
|
1099 | - * |
|
1100 | - * @return string |
|
1101 | - */ |
|
1102 | - public function timespan($other = null, $timezone = null) |
|
1103 | - { |
|
1104 | - if (!$other instanceof DateTimeInterface) { |
|
1105 | - $other = static::parse($other, $timezone); |
|
1106 | - } |
|
1107 | - |
|
1108 | - return $this->diffForHumans($other, [ |
|
1109 | - 'join' => ', ', |
|
1110 | - 'syntax' => CarbonInterface::DIFF_ABSOLUTE, |
|
1111 | - 'options' => CarbonInterface::NO_ZERO_DIFF, |
|
1112 | - 'parts' => -1, |
|
1113 | - ]); |
|
1114 | - } |
|
1115 | - |
|
1116 | - /** |
|
1117 | - * Returns either day of week + time (e.g. "Last Friday at 3:30 PM") if reference time is within 7 days, |
|
1118 | - * or a calendar date (e.g. "10/29/2017") otherwise. |
|
1119 | - * |
|
1120 | - * Language, date and time formats will change according to the current locale. |
|
1121 | - * |
|
1122 | - * @param Carbon|\DateTimeInterface|string|null $referenceTime |
|
1123 | - * @param array $formats |
|
1124 | - * |
|
1125 | - * @return string |
|
1126 | - */ |
|
1127 | - public function calendar($referenceTime = null, array $formats = []) |
|
1128 | - { |
|
1129 | - /** @var CarbonInterface $current */ |
|
1130 | - $current = $this->avoidMutation()->startOfDay(); |
|
1131 | - /** @var CarbonInterface $other */ |
|
1132 | - $other = $this->resolveCarbon($referenceTime)->avoidMutation()->setTimezone($this->getTimezone())->startOfDay(); |
|
1133 | - $diff = $other->diffInDays($current, false); |
|
1134 | - $format = $diff < -6 ? 'sameElse' : ( |
|
1135 | - $diff < -1 ? 'lastWeek' : ( |
|
1136 | - $diff < 0 ? 'lastDay' : ( |
|
1137 | - $diff < 1 ? 'sameDay' : ( |
|
1138 | - $diff < 2 ? 'nextDay' : ( |
|
1139 | - $diff < 7 ? 'nextWeek' : 'sameElse' |
|
1140 | - ) |
|
1141 | - ) |
|
1142 | - ) |
|
1143 | - ) |
|
1144 | - ); |
|
1145 | - $format = array_merge($this->getCalendarFormats(), $formats)[$format]; |
|
1146 | - if ($format instanceof Closure) { |
|
1147 | - $format = $format($current, $other) ?? ''; |
|
1148 | - } |
|
1149 | - |
|
1150 | - return $this->isoFormat((string) $format); |
|
1151 | - } |
|
1152 | - |
|
1153 | - private function getIntervalDayDiff(DateInterval $interval): int |
|
1154 | - { |
|
1155 | - $daysDiff = (int) $interval->format('%a'); |
|
1156 | - $sign = $interval->format('%r') === '-' ? -1 : 1; |
|
1157 | - |
|
1158 | - if (\is_int($interval->days) && |
|
1159 | - $interval->y === 0 && |
|
1160 | - $interval->m === 0 && |
|
1161 | - version_compare(PHP_VERSION, '8.1.0-dev', '<') && |
|
1162 | - abs($interval->d - $daysDiff) === 1 |
|
1163 | - ) { |
|
1164 | - $daysDiff = abs($interval->d); // @codeCoverageIgnore |
|
1165 | - } |
|
1166 | - |
|
1167 | - return $daysDiff * $sign; |
|
1168 | - } |
|
37 | + /** |
|
38 | + * @codeCoverageIgnore |
|
39 | + * |
|
40 | + * @param CarbonInterval $diff |
|
41 | + */ |
|
42 | + protected static function fixNegativeMicroseconds(CarbonInterval $diff) |
|
43 | + { |
|
44 | + if ($diff->s !== 0 || $diff->i !== 0 || $diff->h !== 0 || $diff->d !== 0 || $diff->m !== 0 || $diff->y !== 0) { |
|
45 | + $diff->f = (round($diff->f * 1000000) + 1000000) / 1000000; |
|
46 | + $diff->s--; |
|
47 | + |
|
48 | + if ($diff->s < 0) { |
|
49 | + $diff->s += 60; |
|
50 | + $diff->i--; |
|
51 | + |
|
52 | + if ($diff->i < 0) { |
|
53 | + $diff->i += 60; |
|
54 | + $diff->h--; |
|
55 | + |
|
56 | + if ($diff->h < 0) { |
|
57 | + $diff->h += 24; |
|
58 | + $diff->d--; |
|
59 | + |
|
60 | + if ($diff->d < 0) { |
|
61 | + $diff->d += 30; |
|
62 | + $diff->m--; |
|
63 | + |
|
64 | + if ($diff->m < 0) { |
|
65 | + $diff->m += 12; |
|
66 | + $diff->y--; |
|
67 | + } |
|
68 | + } |
|
69 | + } |
|
70 | + } |
|
71 | + } |
|
72 | + |
|
73 | + return; |
|
74 | + } |
|
75 | + |
|
76 | + $diff->f *= -1; |
|
77 | + $diff->invert(); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @param DateInterval $diff |
|
82 | + * @param bool $absolute |
|
83 | + * |
|
84 | + * @return CarbonInterval |
|
85 | + */ |
|
86 | + protected static function fixDiffInterval(DateInterval $diff, $absolute) |
|
87 | + { |
|
88 | + $diff = CarbonInterval::instance($diff); |
|
89 | + |
|
90 | + // Work-around for https://bugs.php.net/bug.php?id=77145 |
|
91 | + // @codeCoverageIgnoreStart |
|
92 | + if ($diff->f > 0 && $diff->y === -1 && $diff->m === 11 && $diff->d >= 27 && $diff->h === 23 && $diff->i === 59 && $diff->s === 59) { |
|
93 | + $diff->y = 0; |
|
94 | + $diff->m = 0; |
|
95 | + $diff->d = 0; |
|
96 | + $diff->h = 0; |
|
97 | + $diff->i = 0; |
|
98 | + $diff->s = 0; |
|
99 | + $diff->f = (1000000 - round($diff->f * 1000000)) / 1000000; |
|
100 | + $diff->invert(); |
|
101 | + } elseif ($diff->f < 0) { |
|
102 | + static::fixNegativeMicroseconds($diff); |
|
103 | + } |
|
104 | + // @codeCoverageIgnoreEnd |
|
105 | + |
|
106 | + if ($absolute && $diff->invert) { |
|
107 | + $diff->invert(); |
|
108 | + } |
|
109 | + |
|
110 | + return $diff; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get the difference as a DateInterval instance. |
|
115 | + * Return relative interval (negative if $absolute flag is not set to true and the given date is before |
|
116 | + * current one). |
|
117 | + * |
|
118 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
119 | + * @param bool $absolute Get the absolute of the difference |
|
120 | + * |
|
121 | + * @return DateInterval |
|
122 | + */ |
|
123 | + #[ReturnTypeWillChange] |
|
124 | + public function diff($date = null, $absolute = false) |
|
125 | + { |
|
126 | + $other = $this->resolveCarbon($date); |
|
127 | + |
|
128 | + // Work-around for https://bugs.php.net/bug.php?id=81458 |
|
129 | + // It was initially introduced for https://bugs.php.net/bug.php?id=80998 |
|
130 | + // The very specific case of 80998 was fixed in PHP 8.1beta3, but it introduced 81458 |
|
131 | + // So we still need to keep this for now |
|
132 | + // @codeCoverageIgnoreStart |
|
133 | + if (version_compare(PHP_VERSION, '8.1.0-dev', '>=') && $other->tz !== $this->tz) { |
|
134 | + $other = $other->avoidMutation()->tz($this->tz); |
|
135 | + } |
|
136 | + // @codeCoverageIgnoreEnd |
|
137 | + |
|
138 | + return parent::diff($other, (bool) $absolute); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Get the difference as a CarbonInterval instance. |
|
143 | + * Return relative interval (negative if $absolute flag is not set to true and the given date is before |
|
144 | + * current one). |
|
145 | + * |
|
146 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
147 | + * @param bool $absolute Get the absolute of the difference |
|
148 | + * |
|
149 | + * @return CarbonInterval |
|
150 | + */ |
|
151 | + public function diffAsCarbonInterval($date = null, $absolute = true) |
|
152 | + { |
|
153 | + return static::fixDiffInterval($this->diff($this->resolveCarbon($date), $absolute), $absolute); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Get the difference in years |
|
158 | + * |
|
159 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
160 | + * @param bool $absolute Get the absolute of the difference |
|
161 | + * |
|
162 | + * @return int |
|
163 | + */ |
|
164 | + public function diffInYears($date = null, $absolute = true) |
|
165 | + { |
|
166 | + return (int) $this->diff($this->resolveCarbon($date), $absolute)->format('%r%y'); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Get the difference in quarters rounded down. |
|
171 | + * |
|
172 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
173 | + * @param bool $absolute Get the absolute of the difference |
|
174 | + * |
|
175 | + * @return int |
|
176 | + */ |
|
177 | + public function diffInQuarters($date = null, $absolute = true) |
|
178 | + { |
|
179 | + return (int) ($this->diffInMonths($date, $absolute) / static::MONTHS_PER_QUARTER); |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Get the difference in months rounded down. |
|
184 | + * |
|
185 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
186 | + * @param bool $absolute Get the absolute of the difference |
|
187 | + * |
|
188 | + * @return int |
|
189 | + */ |
|
190 | + public function diffInMonths($date = null, $absolute = true) |
|
191 | + { |
|
192 | + $date = $this->resolveCarbon($date); |
|
193 | + |
|
194 | + return $this->diffInYears($date, $absolute) * static::MONTHS_PER_YEAR + (int) $this->diff($date, $absolute)->format('%r%m'); |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Get the difference in weeks rounded down. |
|
199 | + * |
|
200 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
201 | + * @param bool $absolute Get the absolute of the difference |
|
202 | + * |
|
203 | + * @return int |
|
204 | + */ |
|
205 | + public function diffInWeeks($date = null, $absolute = true) |
|
206 | + { |
|
207 | + return (int) ($this->diffInDays($date, $absolute) / static::DAYS_PER_WEEK); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Get the difference in days rounded down. |
|
212 | + * |
|
213 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
214 | + * @param bool $absolute Get the absolute of the difference |
|
215 | + * |
|
216 | + * @return int |
|
217 | + */ |
|
218 | + public function diffInDays($date = null, $absolute = true) |
|
219 | + { |
|
220 | + return $this->getIntervalDayDiff($this->diff($this->resolveCarbon($date), $absolute)); |
|
221 | + } |
|
222 | + |
|
223 | + /** |
|
224 | + * Get the difference in days using a filter closure rounded down. |
|
225 | + * |
|
226 | + * @param Closure $callback |
|
227 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
228 | + * @param bool $absolute Get the absolute of the difference |
|
229 | + * |
|
230 | + * @return int |
|
231 | + */ |
|
232 | + public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = true) |
|
233 | + { |
|
234 | + return $this->diffFiltered(CarbonInterval::day(), $callback, $date, $absolute); |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Get the difference in hours using a filter closure rounded down. |
|
239 | + * |
|
240 | + * @param Closure $callback |
|
241 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
242 | + * @param bool $absolute Get the absolute of the difference |
|
243 | + * |
|
244 | + * @return int |
|
245 | + */ |
|
246 | + public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = true) |
|
247 | + { |
|
248 | + return $this->diffFiltered(CarbonInterval::hour(), $callback, $date, $absolute); |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Get the difference by the given interval using a filter closure. |
|
253 | + * |
|
254 | + * @param CarbonInterval $ci An interval to traverse by |
|
255 | + * @param Closure $callback |
|
256 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
257 | + * @param bool $absolute Get the absolute of the difference |
|
258 | + * |
|
259 | + * @return int |
|
260 | + */ |
|
261 | + public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = true) |
|
262 | + { |
|
263 | + $start = $this; |
|
264 | + $end = $this->resolveCarbon($date); |
|
265 | + $inverse = false; |
|
266 | + |
|
267 | + if ($end < $start) { |
|
268 | + $start = $end; |
|
269 | + $end = $this; |
|
270 | + $inverse = true; |
|
271 | + } |
|
272 | + |
|
273 | + $options = CarbonPeriod::EXCLUDE_END_DATE | ($this->isMutable() ? 0 : CarbonPeriod::IMMUTABLE); |
|
274 | + $diff = $ci->toPeriod($start, $end, $options)->filter($callback)->count(); |
|
275 | + |
|
276 | + return $inverse && !$absolute ? -$diff : $diff; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Get the difference in weekdays rounded down. |
|
281 | + * |
|
282 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
283 | + * @param bool $absolute Get the absolute of the difference |
|
284 | + * |
|
285 | + * @return int |
|
286 | + */ |
|
287 | + public function diffInWeekdays($date = null, $absolute = true) |
|
288 | + { |
|
289 | + return $this->diffInDaysFiltered(function (CarbonInterface $date) { |
|
290 | + return $date->isWeekday(); |
|
291 | + }, $date, $absolute); |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Get the difference in weekend days using a filter rounded down. |
|
296 | + * |
|
297 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
298 | + * @param bool $absolute Get the absolute of the difference |
|
299 | + * |
|
300 | + * @return int |
|
301 | + */ |
|
302 | + public function diffInWeekendDays($date = null, $absolute = true) |
|
303 | + { |
|
304 | + return $this->diffInDaysFiltered(function (CarbonInterface $date) { |
|
305 | + return $date->isWeekend(); |
|
306 | + }, $date, $absolute); |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Get the difference in hours rounded down. |
|
311 | + * |
|
312 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
313 | + * @param bool $absolute Get the absolute of the difference |
|
314 | + * |
|
315 | + * @return int |
|
316 | + */ |
|
317 | + public function diffInHours($date = null, $absolute = true) |
|
318 | + { |
|
319 | + return (int) ($this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR); |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Get the difference in hours rounded down using timestamps. |
|
324 | + * |
|
325 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
326 | + * @param bool $absolute Get the absolute of the difference |
|
327 | + * |
|
328 | + * @return int |
|
329 | + */ |
|
330 | + public function diffInRealHours($date = null, $absolute = true) |
|
331 | + { |
|
332 | + return (int) ($this->diffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR); |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Get the difference in minutes rounded down. |
|
337 | + * |
|
338 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
339 | + * @param bool $absolute Get the absolute of the difference |
|
340 | + * |
|
341 | + * @return int |
|
342 | + */ |
|
343 | + public function diffInMinutes($date = null, $absolute = true) |
|
344 | + { |
|
345 | + return (int) ($this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE); |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * Get the difference in minutes rounded down using timestamps. |
|
350 | + * |
|
351 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
352 | + * @param bool $absolute Get the absolute of the difference |
|
353 | + * |
|
354 | + * @return int |
|
355 | + */ |
|
356 | + public function diffInRealMinutes($date = null, $absolute = true) |
|
357 | + { |
|
358 | + return (int) ($this->diffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE); |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Get the difference in seconds rounded down. |
|
363 | + * |
|
364 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
365 | + * @param bool $absolute Get the absolute of the difference |
|
366 | + * |
|
367 | + * @return int |
|
368 | + */ |
|
369 | + public function diffInSeconds($date = null, $absolute = true) |
|
370 | + { |
|
371 | + $diff = $this->diff($date); |
|
372 | + |
|
373 | + if ($diff->days === 0) { |
|
374 | + $diff = static::fixDiffInterval($diff, $absolute); |
|
375 | + } |
|
376 | + |
|
377 | + $value = (((($diff->m || $diff->y ? $diff->days : $diff->d) * static::HOURS_PER_DAY) + |
|
378 | + $diff->h) * static::MINUTES_PER_HOUR + |
|
379 | + $diff->i) * static::SECONDS_PER_MINUTE + |
|
380 | + $diff->s; |
|
381 | + |
|
382 | + return $absolute || !$diff->invert ? $value : -$value; |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Get the difference in microseconds. |
|
387 | + * |
|
388 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
389 | + * @param bool $absolute Get the absolute of the difference |
|
390 | + * |
|
391 | + * @return int |
|
392 | + */ |
|
393 | + public function diffInMicroseconds($date = null, $absolute = true) |
|
394 | + { |
|
395 | + $diff = $this->diff($date); |
|
396 | + $value = (int) round(((((($diff->m || $diff->y ? $diff->days : $diff->d) * static::HOURS_PER_DAY) + |
|
397 | + $diff->h) * static::MINUTES_PER_HOUR + |
|
398 | + $diff->i) * static::SECONDS_PER_MINUTE + |
|
399 | + ($diff->f + $diff->s)) * static::MICROSECONDS_PER_SECOND); |
|
400 | + |
|
401 | + return $absolute || !$diff->invert ? $value : -$value; |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * Get the difference in milliseconds rounded down. |
|
406 | + * |
|
407 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
408 | + * @param bool $absolute Get the absolute of the difference |
|
409 | + * |
|
410 | + * @return int |
|
411 | + */ |
|
412 | + public function diffInMilliseconds($date = null, $absolute = true) |
|
413 | + { |
|
414 | + return (int) ($this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND); |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Get the difference in seconds using timestamps. |
|
419 | + * |
|
420 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
421 | + * @param bool $absolute Get the absolute of the difference |
|
422 | + * |
|
423 | + * @return int |
|
424 | + */ |
|
425 | + public function diffInRealSeconds($date = null, $absolute = true) |
|
426 | + { |
|
427 | + /** @var CarbonInterface $date */ |
|
428 | + $date = $this->resolveCarbon($date); |
|
429 | + $value = $date->getTimestamp() - $this->getTimestamp(); |
|
430 | + |
|
431 | + return $absolute ? abs($value) : $value; |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Get the difference in microseconds using timestamps. |
|
436 | + * |
|
437 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
438 | + * @param bool $absolute Get the absolute of the difference |
|
439 | + * |
|
440 | + * @return int |
|
441 | + */ |
|
442 | + public function diffInRealMicroseconds($date = null, $absolute = true) |
|
443 | + { |
|
444 | + /** @var CarbonInterface $date */ |
|
445 | + $date = $this->resolveCarbon($date); |
|
446 | + $value = ($date->timestamp - $this->timestamp) * static::MICROSECONDS_PER_SECOND + |
|
447 | + $date->micro - $this->micro; |
|
448 | + |
|
449 | + return $absolute ? abs($value) : $value; |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Get the difference in milliseconds rounded down using timestamps. |
|
454 | + * |
|
455 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
456 | + * @param bool $absolute Get the absolute of the difference |
|
457 | + * |
|
458 | + * @return int |
|
459 | + */ |
|
460 | + public function diffInRealMilliseconds($date = null, $absolute = true) |
|
461 | + { |
|
462 | + return (int) ($this->diffInRealMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND); |
|
463 | + } |
|
464 | + |
|
465 | + /** |
|
466 | + * Get the difference in seconds as float (microsecond-precision). |
|
467 | + * |
|
468 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
469 | + * @param bool $absolute Get the absolute of the difference |
|
470 | + * |
|
471 | + * @return float |
|
472 | + */ |
|
473 | + public function floatDiffInSeconds($date = null, $absolute = true) |
|
474 | + { |
|
475 | + return (float) ($this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_SECOND); |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * Get the difference in minutes as float (microsecond-precision). |
|
480 | + * |
|
481 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
482 | + * @param bool $absolute Get the absolute of the difference |
|
483 | + * |
|
484 | + * @return float |
|
485 | + */ |
|
486 | + public function floatDiffInMinutes($date = null, $absolute = true) |
|
487 | + { |
|
488 | + return $this->floatDiffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * Get the difference in hours as float (microsecond-precision). |
|
493 | + * |
|
494 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
495 | + * @param bool $absolute Get the absolute of the difference |
|
496 | + * |
|
497 | + * @return float |
|
498 | + */ |
|
499 | + public function floatDiffInHours($date = null, $absolute = true) |
|
500 | + { |
|
501 | + return $this->floatDiffInMinutes($date, $absolute) / static::MINUTES_PER_HOUR; |
|
502 | + } |
|
503 | + |
|
504 | + /** |
|
505 | + * Get the difference in days as float (microsecond-precision). |
|
506 | + * |
|
507 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
508 | + * @param bool $absolute Get the absolute of the difference |
|
509 | + * |
|
510 | + * @return float |
|
511 | + */ |
|
512 | + public function floatDiffInDays($date = null, $absolute = true) |
|
513 | + { |
|
514 | + $hoursDiff = $this->floatDiffInHours($date, $absolute); |
|
515 | + $interval = $this->diff($date, $absolute); |
|
516 | + |
|
517 | + if ($interval->y === 0 && $interval->m === 0 && $interval->d === 0) { |
|
518 | + return $hoursDiff / static::HOURS_PER_DAY; |
|
519 | + } |
|
520 | + |
|
521 | + $daysDiff = $this->getIntervalDayDiff($interval); |
|
522 | + |
|
523 | + return $daysDiff + fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; |
|
524 | + } |
|
525 | + |
|
526 | + /** |
|
527 | + * Get the difference in weeks as float (microsecond-precision). |
|
528 | + * |
|
529 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
530 | + * @param bool $absolute Get the absolute of the difference |
|
531 | + * |
|
532 | + * @return float |
|
533 | + */ |
|
534 | + public function floatDiffInWeeks($date = null, $absolute = true) |
|
535 | + { |
|
536 | + return $this->floatDiffInDays($date, $absolute) / static::DAYS_PER_WEEK; |
|
537 | + } |
|
538 | + |
|
539 | + /** |
|
540 | + * Get the difference in months as float (microsecond-precision). |
|
541 | + * |
|
542 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
543 | + * @param bool $absolute Get the absolute of the difference |
|
544 | + * |
|
545 | + * @return float |
|
546 | + */ |
|
547 | + public function floatDiffInMonths($date = null, $absolute = true) |
|
548 | + { |
|
549 | + $start = $this; |
|
550 | + $end = $this->resolveCarbon($date); |
|
551 | + $ascending = ($start <= $end); |
|
552 | + $sign = $absolute || $ascending ? 1 : -1; |
|
553 | + if (!$ascending) { |
|
554 | + [$start, $end] = [$end, $start]; |
|
555 | + } |
|
556 | + $monthsDiff = $start->diffInMonths($end); |
|
557 | + /** @var Carbon|CarbonImmutable $floorEnd */ |
|
558 | + $floorEnd = $start->avoidMutation()->addMonths($monthsDiff); |
|
559 | + |
|
560 | + if ($floorEnd >= $end) { |
|
561 | + return $sign * $monthsDiff; |
|
562 | + } |
|
563 | + |
|
564 | + /** @var Carbon|CarbonImmutable $startOfMonthAfterFloorEnd */ |
|
565 | + $startOfMonthAfterFloorEnd = $floorEnd->avoidMutation()->addMonth()->startOfMonth(); |
|
566 | + |
|
567 | + if ($startOfMonthAfterFloorEnd > $end) { |
|
568 | + return $sign * ($monthsDiff + $floorEnd->floatDiffInDays($end) / $floorEnd->daysInMonth); |
|
569 | + } |
|
570 | + |
|
571 | + return $sign * ($monthsDiff + $floorEnd->floatDiffInDays($startOfMonthAfterFloorEnd) / $floorEnd->daysInMonth + $startOfMonthAfterFloorEnd->floatDiffInDays($end) / $end->daysInMonth); |
|
572 | + } |
|
573 | + |
|
574 | + /** |
|
575 | + * Get the difference in year as float (microsecond-precision). |
|
576 | + * |
|
577 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
578 | + * @param bool $absolute Get the absolute of the difference |
|
579 | + * |
|
580 | + * @return float |
|
581 | + */ |
|
582 | + public function floatDiffInYears($date = null, $absolute = true) |
|
583 | + { |
|
584 | + $start = $this; |
|
585 | + $end = $this->resolveCarbon($date); |
|
586 | + $ascending = ($start <= $end); |
|
587 | + $sign = $absolute || $ascending ? 1 : -1; |
|
588 | + if (!$ascending) { |
|
589 | + [$start, $end] = [$end, $start]; |
|
590 | + } |
|
591 | + $yearsDiff = $start->diffInYears($end); |
|
592 | + /** @var Carbon|CarbonImmutable $floorEnd */ |
|
593 | + $floorEnd = $start->avoidMutation()->addYears($yearsDiff); |
|
594 | + |
|
595 | + if ($floorEnd >= $end) { |
|
596 | + return $sign * $yearsDiff; |
|
597 | + } |
|
598 | + |
|
599 | + /** @var Carbon|CarbonImmutable $startOfYearAfterFloorEnd */ |
|
600 | + $startOfYearAfterFloorEnd = $floorEnd->avoidMutation()->addYear()->startOfYear(); |
|
601 | + |
|
602 | + if ($startOfYearAfterFloorEnd > $end) { |
|
603 | + return $sign * ($yearsDiff + $floorEnd->floatDiffInDays($end) / $floorEnd->daysInYear); |
|
604 | + } |
|
605 | + |
|
606 | + return $sign * ($yearsDiff + $floorEnd->floatDiffInDays($startOfYearAfterFloorEnd) / $floorEnd->daysInYear + $startOfYearAfterFloorEnd->floatDiffInDays($end) / $end->daysInYear); |
|
607 | + } |
|
608 | + |
|
609 | + /** |
|
610 | + * Get the difference in seconds as float (microsecond-precision) using timestamps. |
|
611 | + * |
|
612 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
613 | + * @param bool $absolute Get the absolute of the difference |
|
614 | + * |
|
615 | + * @return float |
|
616 | + */ |
|
617 | + public function floatDiffInRealSeconds($date = null, $absolute = true) |
|
618 | + { |
|
619 | + return $this->diffInRealMicroseconds($date, $absolute) / static::MICROSECONDS_PER_SECOND; |
|
620 | + } |
|
621 | + |
|
622 | + /** |
|
623 | + * Get the difference in minutes as float (microsecond-precision) using timestamps. |
|
624 | + * |
|
625 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
626 | + * @param bool $absolute Get the absolute of the difference |
|
627 | + * |
|
628 | + * @return float |
|
629 | + */ |
|
630 | + public function floatDiffInRealMinutes($date = null, $absolute = true) |
|
631 | + { |
|
632 | + return $this->floatDiffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; |
|
633 | + } |
|
634 | + |
|
635 | + /** |
|
636 | + * Get the difference in hours as float (microsecond-precision) using timestamps. |
|
637 | + * |
|
638 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
639 | + * @param bool $absolute Get the absolute of the difference |
|
640 | + * |
|
641 | + * @return float |
|
642 | + */ |
|
643 | + public function floatDiffInRealHours($date = null, $absolute = true) |
|
644 | + { |
|
645 | + return $this->floatDiffInRealMinutes($date, $absolute) / static::MINUTES_PER_HOUR; |
|
646 | + } |
|
647 | + |
|
648 | + /** |
|
649 | + * Get the difference in days as float (microsecond-precision). |
|
650 | + * |
|
651 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
652 | + * @param bool $absolute Get the absolute of the difference |
|
653 | + * |
|
654 | + * @return float |
|
655 | + */ |
|
656 | + public function floatDiffInRealDays($date = null, $absolute = true) |
|
657 | + { |
|
658 | + $date = $this->resolveUTC($date); |
|
659 | + $utc = $this->avoidMutation()->utc(); |
|
660 | + $hoursDiff = $utc->floatDiffInRealHours($date, $absolute); |
|
661 | + |
|
662 | + return ($hoursDiff < 0 ? -1 : 1) * $utc->diffInDays($date) + fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; |
|
663 | + } |
|
664 | + |
|
665 | + /** |
|
666 | + * Get the difference in weeks as float (microsecond-precision). |
|
667 | + * |
|
668 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
669 | + * @param bool $absolute Get the absolute of the difference |
|
670 | + * |
|
671 | + * @return float |
|
672 | + */ |
|
673 | + public function floatDiffInRealWeeks($date = null, $absolute = true) |
|
674 | + { |
|
675 | + return $this->floatDiffInRealDays($date, $absolute) / static::DAYS_PER_WEEK; |
|
676 | + } |
|
677 | + |
|
678 | + /** |
|
679 | + * Get the difference in months as float (microsecond-precision) using timestamps. |
|
680 | + * |
|
681 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
682 | + * @param bool $absolute Get the absolute of the difference |
|
683 | + * |
|
684 | + * @return float |
|
685 | + */ |
|
686 | + public function floatDiffInRealMonths($date = null, $absolute = true) |
|
687 | + { |
|
688 | + $start = $this; |
|
689 | + $end = $this->resolveCarbon($date); |
|
690 | + $ascending = ($start <= $end); |
|
691 | + $sign = $absolute || $ascending ? 1 : -1; |
|
692 | + if (!$ascending) { |
|
693 | + [$start, $end] = [$end, $start]; |
|
694 | + } |
|
695 | + $monthsDiff = $start->diffInMonths($end); |
|
696 | + /** @var Carbon|CarbonImmutable $floorEnd */ |
|
697 | + $floorEnd = $start->avoidMutation()->addMonths($monthsDiff); |
|
698 | + |
|
699 | + if ($floorEnd >= $end) { |
|
700 | + return $sign * $monthsDiff; |
|
701 | + } |
|
702 | + |
|
703 | + /** @var Carbon|CarbonImmutable $startOfMonthAfterFloorEnd */ |
|
704 | + $startOfMonthAfterFloorEnd = $floorEnd->avoidMutation()->addMonth()->startOfMonth(); |
|
705 | + |
|
706 | + if ($startOfMonthAfterFloorEnd > $end) { |
|
707 | + return $sign * ($monthsDiff + $floorEnd->floatDiffInRealDays($end) / $floorEnd->daysInMonth); |
|
708 | + } |
|
709 | + |
|
710 | + return $sign * ($monthsDiff + $floorEnd->floatDiffInRealDays($startOfMonthAfterFloorEnd) / $floorEnd->daysInMonth + $startOfMonthAfterFloorEnd->floatDiffInRealDays($end) / $end->daysInMonth); |
|
711 | + } |
|
712 | + |
|
713 | + /** |
|
714 | + * Get the difference in year as float (microsecond-precision) using timestamps. |
|
715 | + * |
|
716 | + * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date |
|
717 | + * @param bool $absolute Get the absolute of the difference |
|
718 | + * |
|
719 | + * @return float |
|
720 | + */ |
|
721 | + public function floatDiffInRealYears($date = null, $absolute = true) |
|
722 | + { |
|
723 | + $start = $this; |
|
724 | + $end = $this->resolveCarbon($date); |
|
725 | + $ascending = ($start <= $end); |
|
726 | + $sign = $absolute || $ascending ? 1 : -1; |
|
727 | + if (!$ascending) { |
|
728 | + [$start, $end] = [$end, $start]; |
|
729 | + } |
|
730 | + $yearsDiff = $start->diffInYears($end); |
|
731 | + /** @var Carbon|CarbonImmutable $floorEnd */ |
|
732 | + $floorEnd = $start->avoidMutation()->addYears($yearsDiff); |
|
733 | + |
|
734 | + if ($floorEnd >= $end) { |
|
735 | + return $sign * $yearsDiff; |
|
736 | + } |
|
737 | + |
|
738 | + /** @var Carbon|CarbonImmutable $startOfYearAfterFloorEnd */ |
|
739 | + $startOfYearAfterFloorEnd = $floorEnd->avoidMutation()->addYear()->startOfYear(); |
|
740 | + |
|
741 | + if ($startOfYearAfterFloorEnd > $end) { |
|
742 | + return $sign * ($yearsDiff + $floorEnd->floatDiffInRealDays($end) / $floorEnd->daysInYear); |
|
743 | + } |
|
744 | + |
|
745 | + return $sign * ($yearsDiff + $floorEnd->floatDiffInRealDays($startOfYearAfterFloorEnd) / $floorEnd->daysInYear + $startOfYearAfterFloorEnd->floatDiffInRealDays($end) / $end->daysInYear); |
|
746 | + } |
|
747 | + |
|
748 | + /** |
|
749 | + * The number of seconds since midnight. |
|
750 | + * |
|
751 | + * @return int |
|
752 | + */ |
|
753 | + public function secondsSinceMidnight() |
|
754 | + { |
|
755 | + return $this->diffInSeconds($this->avoidMutation()->startOfDay()); |
|
756 | + } |
|
757 | + |
|
758 | + /** |
|
759 | + * The number of seconds until 23:59:59. |
|
760 | + * |
|
761 | + * @return int |
|
762 | + */ |
|
763 | + public function secondsUntilEndOfDay() |
|
764 | + { |
|
765 | + return $this->diffInSeconds($this->avoidMutation()->endOfDay()); |
|
766 | + } |
|
767 | + |
|
768 | + /** |
|
769 | + * Get the difference in a human readable format in the current locale from current instance to an other |
|
770 | + * instance given (or now if null given). |
|
771 | + * |
|
772 | + * @example |
|
773 | + * ``` |
|
774 | + * echo Carbon::tomorrow()->diffForHumans() . "\n"; |
|
775 | + * echo Carbon::tomorrow()->diffForHumans(['parts' => 2]) . "\n"; |
|
776 | + * echo Carbon::tomorrow()->diffForHumans(['parts' => 3, 'join' => true]) . "\n"; |
|
777 | + * echo Carbon::tomorrow()->diffForHumans(Carbon::yesterday()) . "\n"; |
|
778 | + * echo Carbon::tomorrow()->diffForHumans(Carbon::yesterday(), ['short' => true]) . "\n"; |
|
779 | + * ``` |
|
780 | + * |
|
781 | + * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below; |
|
782 | + * if null passed, now will be used as comparison reference; |
|
783 | + * if any other type, it will be converted to date and used as reference. |
|
784 | + * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
785 | + * - 'syntax' entry (see below) |
|
786 | + * - 'short' entry (see below) |
|
787 | + * - 'parts' entry (see below) |
|
788 | + * - 'options' entry (see below) |
|
789 | + * - 'skip' entry, list of units to skip (array of strings or a single string, |
|
790 | + * ` it can be the unit name (singular or plural) or its shortcut |
|
791 | + * ` (y, m, w, d, h, min, s, ms, µs). |
|
792 | + * - 'aUnit' entry, prefer "an hour" over "1 hour" if true |
|
793 | + * - 'join' entry determines how to join multiple parts of the string |
|
794 | + * ` - if $join is a string, it's used as a joiner glue |
|
795 | + * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
796 | + * ` - if $join is an array, the first item will be the default glue, and the second item |
|
797 | + * ` will be used instead of the glue for the last item |
|
798 | + * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
799 | + * ` - if $join is missing, a space will be used as glue |
|
800 | + * - 'other' entry (see above) |
|
801 | + * - 'minimumUnit' entry determines the smallest unit of time to display can be long or |
|
802 | + * ` short form of the units, e.g. 'hour' or 'h' (default value: s) |
|
803 | + * if int passed, it add modifiers: |
|
804 | + * Possible values: |
|
805 | + * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
806 | + * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
807 | + * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
808 | + * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
809 | + * @param bool $short displays short format of time units |
|
810 | + * @param int $parts maximum number of parts to display (default value: 1: single unit) |
|
811 | + * @param int $options human diff options |
|
812 | + * |
|
813 | + * @return string |
|
814 | + */ |
|
815 | + public function diffForHumans($other = null, $syntax = null, $short = false, $parts = 1, $options = null) |
|
816 | + { |
|
817 | + /* @var CarbonInterface $this */ |
|
818 | + if (\is_array($other)) { |
|
819 | + $other['syntax'] = \array_key_exists('syntax', $other) ? $other['syntax'] : $syntax; |
|
820 | + $syntax = $other; |
|
821 | + $other = $syntax['other'] ?? null; |
|
822 | + } |
|
823 | + |
|
824 | + $intSyntax = &$syntax; |
|
825 | + if (\is_array($syntax)) { |
|
826 | + $syntax['syntax'] = $syntax['syntax'] ?? null; |
|
827 | + $intSyntax = &$syntax['syntax']; |
|
828 | + } |
|
829 | + $intSyntax = (int) ($intSyntax ?? static::DIFF_RELATIVE_AUTO); |
|
830 | + $intSyntax = $intSyntax === static::DIFF_RELATIVE_AUTO && $other === null ? static::DIFF_RELATIVE_TO_NOW : $intSyntax; |
|
831 | + |
|
832 | + $parts = min(7, max(1, (int) $parts)); |
|
833 | + |
|
834 | + return $this->diffAsCarbonInterval($other, false) |
|
835 | + ->setLocalTranslator($this->getLocalTranslator()) |
|
836 | + ->forHumans($syntax, (bool) $short, $parts, $options ?? $this->localHumanDiffOptions ?? static::getHumanDiffOptions()); |
|
837 | + } |
|
838 | + |
|
839 | + /** |
|
840 | + * @alias diffForHumans |
|
841 | + * |
|
842 | + * Get the difference in a human readable format in the current locale from current instance to an other |
|
843 | + * instance given (or now if null given). |
|
844 | + * |
|
845 | + * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below; |
|
846 | + * if null passed, now will be used as comparison reference; |
|
847 | + * if any other type, it will be converted to date and used as reference. |
|
848 | + * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
849 | + * - 'syntax' entry (see below) |
|
850 | + * - 'short' entry (see below) |
|
851 | + * - 'parts' entry (see below) |
|
852 | + * - 'options' entry (see below) |
|
853 | + * - 'join' entry determines how to join multiple parts of the string |
|
854 | + * ` - if $join is a string, it's used as a joiner glue |
|
855 | + * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
856 | + * ` - if $join is an array, the first item will be the default glue, and the second item |
|
857 | + * ` will be used instead of the glue for the last item |
|
858 | + * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
859 | + * ` - if $join is missing, a space will be used as glue |
|
860 | + * - 'other' entry (see above) |
|
861 | + * if int passed, it add modifiers: |
|
862 | + * Possible values: |
|
863 | + * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
864 | + * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
865 | + * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
866 | + * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
867 | + * @param bool $short displays short format of time units |
|
868 | + * @param int $parts maximum number of parts to display (default value: 1: single unit) |
|
869 | + * @param int $options human diff options |
|
870 | + * |
|
871 | + * @return string |
|
872 | + */ |
|
873 | + public function from($other = null, $syntax = null, $short = false, $parts = 1, $options = null) |
|
874 | + { |
|
875 | + return $this->diffForHumans($other, $syntax, $short, $parts, $options); |
|
876 | + } |
|
877 | + |
|
878 | + /** |
|
879 | + * @alias diffForHumans |
|
880 | + * |
|
881 | + * Get the difference in a human readable format in the current locale from current instance to an other |
|
882 | + * instance given (or now if null given). |
|
883 | + */ |
|
884 | + public function since($other = null, $syntax = null, $short = false, $parts = 1, $options = null) |
|
885 | + { |
|
886 | + return $this->diffForHumans($other, $syntax, $short, $parts, $options); |
|
887 | + } |
|
888 | + |
|
889 | + /** |
|
890 | + * Get the difference in a human readable format in the current locale from an other |
|
891 | + * instance given (or now if null given) to current instance. |
|
892 | + * |
|
893 | + * When comparing a value in the past to default now: |
|
894 | + * 1 hour from now |
|
895 | + * 5 months from now |
|
896 | + * |
|
897 | + * When comparing a value in the future to default now: |
|
898 | + * 1 hour ago |
|
899 | + * 5 months ago |
|
900 | + * |
|
901 | + * When comparing a value in the past to another value: |
|
902 | + * 1 hour after |
|
903 | + * 5 months after |
|
904 | + * |
|
905 | + * When comparing a value in the future to another value: |
|
906 | + * 1 hour before |
|
907 | + * 5 months before |
|
908 | + * |
|
909 | + * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below; |
|
910 | + * if null passed, now will be used as comparison reference; |
|
911 | + * if any other type, it will be converted to date and used as reference. |
|
912 | + * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
913 | + * - 'syntax' entry (see below) |
|
914 | + * - 'short' entry (see below) |
|
915 | + * - 'parts' entry (see below) |
|
916 | + * - 'options' entry (see below) |
|
917 | + * - 'join' entry determines how to join multiple parts of the string |
|
918 | + * ` - if $join is a string, it's used as a joiner glue |
|
919 | + * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
920 | + * ` - if $join is an array, the first item will be the default glue, and the second item |
|
921 | + * ` will be used instead of the glue for the last item |
|
922 | + * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
923 | + * ` - if $join is missing, a space will be used as glue |
|
924 | + * - 'other' entry (see above) |
|
925 | + * if int passed, it add modifiers: |
|
926 | + * Possible values: |
|
927 | + * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
928 | + * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
929 | + * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
930 | + * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
931 | + * @param bool $short displays short format of time units |
|
932 | + * @param int $parts maximum number of parts to display (default value: 1: single unit) |
|
933 | + * @param int $options human diff options |
|
934 | + * |
|
935 | + * @return string |
|
936 | + */ |
|
937 | + public function to($other = null, $syntax = null, $short = false, $parts = 1, $options = null) |
|
938 | + { |
|
939 | + if (!$syntax && !$other) { |
|
940 | + $syntax = CarbonInterface::DIFF_RELATIVE_TO_NOW; |
|
941 | + } |
|
942 | + |
|
943 | + return $this->resolveCarbon($other)->diffForHumans($this, $syntax, $short, $parts, $options); |
|
944 | + } |
|
945 | + |
|
946 | + /** |
|
947 | + * @alias to |
|
948 | + * |
|
949 | + * Get the difference in a human readable format in the current locale from an other |
|
950 | + * instance given (or now if null given) to current instance. |
|
951 | + * |
|
952 | + * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below; |
|
953 | + * if null passed, now will be used as comparison reference; |
|
954 | + * if any other type, it will be converted to date and used as reference. |
|
955 | + * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
956 | + * - 'syntax' entry (see below) |
|
957 | + * - 'short' entry (see below) |
|
958 | + * - 'parts' entry (see below) |
|
959 | + * - 'options' entry (see below) |
|
960 | + * - 'join' entry determines how to join multiple parts of the string |
|
961 | + * ` - if $join is a string, it's used as a joiner glue |
|
962 | + * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
963 | + * ` - if $join is an array, the first item will be the default glue, and the second item |
|
964 | + * ` will be used instead of the glue for the last item |
|
965 | + * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
966 | + * ` - if $join is missing, a space will be used as glue |
|
967 | + * - 'other' entry (see above) |
|
968 | + * if int passed, it add modifiers: |
|
969 | + * Possible values: |
|
970 | + * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
971 | + * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
972 | + * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
973 | + * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
974 | + * @param bool $short displays short format of time units |
|
975 | + * @param int $parts maximum number of parts to display (default value: 1: single unit) |
|
976 | + * @param int $options human diff options |
|
977 | + * |
|
978 | + * @return string |
|
979 | + */ |
|
980 | + public function until($other = null, $syntax = null, $short = false, $parts = 1, $options = null) |
|
981 | + { |
|
982 | + return $this->to($other, $syntax, $short, $parts, $options); |
|
983 | + } |
|
984 | + |
|
985 | + /** |
|
986 | + * Get the difference in a human readable format in the current locale from current |
|
987 | + * instance to now. |
|
988 | + * |
|
989 | + * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
990 | + * - 'syntax' entry (see below) |
|
991 | + * - 'short' entry (see below) |
|
992 | + * - 'parts' entry (see below) |
|
993 | + * - 'options' entry (see below) |
|
994 | + * - 'join' entry determines how to join multiple parts of the string |
|
995 | + * ` - if $join is a string, it's used as a joiner glue |
|
996 | + * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
997 | + * ` - if $join is an array, the first item will be the default glue, and the second item |
|
998 | + * ` will be used instead of the glue for the last item |
|
999 | + * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
1000 | + * ` - if $join is missing, a space will be used as glue |
|
1001 | + * if int passed, it add modifiers: |
|
1002 | + * Possible values: |
|
1003 | + * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
1004 | + * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
1005 | + * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
1006 | + * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
1007 | + * @param bool $short displays short format of time units |
|
1008 | + * @param int $parts maximum number of parts to display (default value: 1: single unit) |
|
1009 | + * @param int $options human diff options |
|
1010 | + * |
|
1011 | + * @return string |
|
1012 | + */ |
|
1013 | + public function fromNow($syntax = null, $short = false, $parts = 1, $options = null) |
|
1014 | + { |
|
1015 | + $other = null; |
|
1016 | + |
|
1017 | + if ($syntax instanceof DateTimeInterface) { |
|
1018 | + [$other, $syntax, $short, $parts, $options] = array_pad(\func_get_args(), 5, null); |
|
1019 | + } |
|
1020 | + |
|
1021 | + return $this->from($other, $syntax, $short, $parts, $options); |
|
1022 | + } |
|
1023 | + |
|
1024 | + /** |
|
1025 | + * Get the difference in a human readable format in the current locale from an other |
|
1026 | + * instance given to now |
|
1027 | + * |
|
1028 | + * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
1029 | + * - 'syntax' entry (see below) |
|
1030 | + * - 'short' entry (see below) |
|
1031 | + * - 'parts' entry (see below) |
|
1032 | + * - 'options' entry (see below) |
|
1033 | + * - 'join' entry determines how to join multiple parts of the string |
|
1034 | + * ` - if $join is a string, it's used as a joiner glue |
|
1035 | + * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
1036 | + * ` - if $join is an array, the first item will be the default glue, and the second item |
|
1037 | + * ` will be used instead of the glue for the last item |
|
1038 | + * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
1039 | + * ` - if $join is missing, a space will be used as glue |
|
1040 | + * if int passed, it add modifiers: |
|
1041 | + * Possible values: |
|
1042 | + * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
1043 | + * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
1044 | + * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
1045 | + * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
1046 | + * @param bool $short displays short format of time units |
|
1047 | + * @param int $parts maximum number of parts to display (default value: 1: single part) |
|
1048 | + * @param int $options human diff options |
|
1049 | + * |
|
1050 | + * @return string |
|
1051 | + */ |
|
1052 | + public function toNow($syntax = null, $short = false, $parts = 1, $options = null) |
|
1053 | + { |
|
1054 | + return $this->to(null, $syntax, $short, $parts, $options); |
|
1055 | + } |
|
1056 | + |
|
1057 | + /** |
|
1058 | + * Get the difference in a human readable format in the current locale from an other |
|
1059 | + * instance given to now |
|
1060 | + * |
|
1061 | + * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains: |
|
1062 | + * - 'syntax' entry (see below) |
|
1063 | + * - 'short' entry (see below) |
|
1064 | + * - 'parts' entry (see below) |
|
1065 | + * - 'options' entry (see below) |
|
1066 | + * - 'join' entry determines how to join multiple parts of the string |
|
1067 | + * ` - if $join is a string, it's used as a joiner glue |
|
1068 | + * ` - if $join is a callable/closure, it get the list of string and should return a string |
|
1069 | + * ` - if $join is an array, the first item will be the default glue, and the second item |
|
1070 | + * ` will be used instead of the glue for the last item |
|
1071 | + * ` - if $join is true, it will be guessed from the locale ('list' translation file entry) |
|
1072 | + * ` - if $join is missing, a space will be used as glue |
|
1073 | + * if int passed, it add modifiers: |
|
1074 | + * Possible values: |
|
1075 | + * - CarbonInterface::DIFF_ABSOLUTE no modifiers |
|
1076 | + * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier |
|
1077 | + * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier |
|
1078 | + * Default value: CarbonInterface::DIFF_ABSOLUTE |
|
1079 | + * @param bool $short displays short format of time units |
|
1080 | + * @param int $parts maximum number of parts to display (default value: 1: single part) |
|
1081 | + * @param int $options human diff options |
|
1082 | + * |
|
1083 | + * @return string |
|
1084 | + */ |
|
1085 | + public function ago($syntax = null, $short = false, $parts = 1, $options = null) |
|
1086 | + { |
|
1087 | + $other = null; |
|
1088 | + |
|
1089 | + if ($syntax instanceof DateTimeInterface) { |
|
1090 | + [$other, $syntax, $short, $parts, $options] = array_pad(\func_get_args(), 5, null); |
|
1091 | + } |
|
1092 | + |
|
1093 | + return $this->from($other, $syntax, $short, $parts, $options); |
|
1094 | + } |
|
1095 | + |
|
1096 | + /** |
|
1097 | + * Get the difference in a human readable format in the current locale from current instance to an other |
|
1098 | + * instance given (or now if null given). |
|
1099 | + * |
|
1100 | + * @return string |
|
1101 | + */ |
|
1102 | + public function timespan($other = null, $timezone = null) |
|
1103 | + { |
|
1104 | + if (!$other instanceof DateTimeInterface) { |
|
1105 | + $other = static::parse($other, $timezone); |
|
1106 | + } |
|
1107 | + |
|
1108 | + return $this->diffForHumans($other, [ |
|
1109 | + 'join' => ', ', |
|
1110 | + 'syntax' => CarbonInterface::DIFF_ABSOLUTE, |
|
1111 | + 'options' => CarbonInterface::NO_ZERO_DIFF, |
|
1112 | + 'parts' => -1, |
|
1113 | + ]); |
|
1114 | + } |
|
1115 | + |
|
1116 | + /** |
|
1117 | + * Returns either day of week + time (e.g. "Last Friday at 3:30 PM") if reference time is within 7 days, |
|
1118 | + * or a calendar date (e.g. "10/29/2017") otherwise. |
|
1119 | + * |
|
1120 | + * Language, date and time formats will change according to the current locale. |
|
1121 | + * |
|
1122 | + * @param Carbon|\DateTimeInterface|string|null $referenceTime |
|
1123 | + * @param array $formats |
|
1124 | + * |
|
1125 | + * @return string |
|
1126 | + */ |
|
1127 | + public function calendar($referenceTime = null, array $formats = []) |
|
1128 | + { |
|
1129 | + /** @var CarbonInterface $current */ |
|
1130 | + $current = $this->avoidMutation()->startOfDay(); |
|
1131 | + /** @var CarbonInterface $other */ |
|
1132 | + $other = $this->resolveCarbon($referenceTime)->avoidMutation()->setTimezone($this->getTimezone())->startOfDay(); |
|
1133 | + $diff = $other->diffInDays($current, false); |
|
1134 | + $format = $diff < -6 ? 'sameElse' : ( |
|
1135 | + $diff < -1 ? 'lastWeek' : ( |
|
1136 | + $diff < 0 ? 'lastDay' : ( |
|
1137 | + $diff < 1 ? 'sameDay' : ( |
|
1138 | + $diff < 2 ? 'nextDay' : ( |
|
1139 | + $diff < 7 ? 'nextWeek' : 'sameElse' |
|
1140 | + ) |
|
1141 | + ) |
|
1142 | + ) |
|
1143 | + ) |
|
1144 | + ); |
|
1145 | + $format = array_merge($this->getCalendarFormats(), $formats)[$format]; |
|
1146 | + if ($format instanceof Closure) { |
|
1147 | + $format = $format($current, $other) ?? ''; |
|
1148 | + } |
|
1149 | + |
|
1150 | + return $this->isoFormat((string) $format); |
|
1151 | + } |
|
1152 | + |
|
1153 | + private function getIntervalDayDiff(DateInterval $interval): int |
|
1154 | + { |
|
1155 | + $daysDiff = (int) $interval->format('%a'); |
|
1156 | + $sign = $interval->format('%r') === '-' ? -1 : 1; |
|
1157 | + |
|
1158 | + if (\is_int($interval->days) && |
|
1159 | + $interval->y === 0 && |
|
1160 | + $interval->m === 0 && |
|
1161 | + version_compare(PHP_VERSION, '8.1.0-dev', '<') && |
|
1162 | + abs($interval->d - $daysDiff) === 1 |
|
1163 | + ) { |
|
1164 | + $daysDiff = abs($interval->d); // @codeCoverageIgnore |
|
1165 | + } |
|
1166 | + |
|
1167 | + return $daysDiff * $sign; |
|
1168 | + } |
|
1169 | 1169 | } |
@@ -286,7 +286,7 @@ discard block |
||
286 | 286 | */ |
287 | 287 | public function diffInWeekdays($date = null, $absolute = true) |
288 | 288 | { |
289 | - return $this->diffInDaysFiltered(function (CarbonInterface $date) { |
|
289 | + return $this->diffInDaysFiltered(function(CarbonInterface $date) { |
|
290 | 290 | return $date->isWeekday(); |
291 | 291 | }, $date, $absolute); |
292 | 292 | } |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | */ |
302 | 302 | public function diffInWeekendDays($date = null, $absolute = true) |
303 | 303 | { |
304 | - return $this->diffInDaysFiltered(function (CarbonInterface $date) { |
|
304 | + return $this->diffInDaysFiltered(function(CarbonInterface $date) { |
|
305 | 305 | return $date->isWeekend(); |
306 | 306 | }, $date, $absolute); |
307 | 307 | } |
@@ -26,379 +26,379 @@ |
||
26 | 26 | */ |
27 | 27 | trait Units |
28 | 28 | { |
29 | - /** |
|
30 | - * Add seconds to the instance using timestamp. Positive $value travels |
|
31 | - * forward while negative $value travels into the past. |
|
32 | - * |
|
33 | - * @param string $unit |
|
34 | - * @param int $value |
|
35 | - * |
|
36 | - * @return static |
|
37 | - */ |
|
38 | - public function addRealUnit($unit, $value = 1) |
|
39 | - { |
|
40 | - switch ($unit) { |
|
41 | - // @call addRealUnit |
|
42 | - case 'micro': |
|
29 | + /** |
|
30 | + * Add seconds to the instance using timestamp. Positive $value travels |
|
31 | + * forward while negative $value travels into the past. |
|
32 | + * |
|
33 | + * @param string $unit |
|
34 | + * @param int $value |
|
35 | + * |
|
36 | + * @return static |
|
37 | + */ |
|
38 | + public function addRealUnit($unit, $value = 1) |
|
39 | + { |
|
40 | + switch ($unit) { |
|
41 | + // @call addRealUnit |
|
42 | + case 'micro': |
|
43 | 43 | |
44 | - // @call addRealUnit |
|
45 | - case 'microsecond': |
|
46 | - /* @var CarbonInterface $this */ |
|
47 | - $diff = $this->microsecond + $value; |
|
48 | - $time = $this->getTimestamp(); |
|
49 | - $seconds = (int) floor($diff / static::MICROSECONDS_PER_SECOND); |
|
50 | - $time += $seconds; |
|
51 | - $diff -= $seconds * static::MICROSECONDS_PER_SECOND; |
|
52 | - $microtime = str_pad((string) $diff, 6, '0', STR_PAD_LEFT); |
|
53 | - $tz = $this->tz; |
|
54 | - |
|
55 | - return $this->tz('UTC')->modify("@$time.$microtime")->tz($tz); |
|
56 | - |
|
57 | - // @call addRealUnit |
|
58 | - case 'milli': |
|
59 | - // @call addRealUnit |
|
60 | - case 'millisecond': |
|
61 | - return $this->addRealUnit('microsecond', $value * static::MICROSECONDS_PER_MILLISECOND); |
|
62 | - |
|
63 | - // @call addRealUnit |
|
64 | - case 'second': |
|
65 | - break; |
|
66 | - |
|
67 | - // @call addRealUnit |
|
68 | - case 'minute': |
|
69 | - $value *= static::SECONDS_PER_MINUTE; |
|
70 | - |
|
71 | - break; |
|
72 | - |
|
73 | - // @call addRealUnit |
|
74 | - case 'hour': |
|
75 | - $value *= static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
76 | - |
|
77 | - break; |
|
78 | - |
|
79 | - // @call addRealUnit |
|
80 | - case 'day': |
|
81 | - $value *= static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
82 | - |
|
83 | - break; |
|
84 | - |
|
85 | - // @call addRealUnit |
|
86 | - case 'week': |
|
87 | - $value *= static::DAYS_PER_WEEK * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
88 | - |
|
89 | - break; |
|
90 | - |
|
91 | - // @call addRealUnit |
|
92 | - case 'month': |
|
93 | - $value *= 30 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
94 | - |
|
95 | - break; |
|
96 | - |
|
97 | - // @call addRealUnit |
|
98 | - case 'quarter': |
|
99 | - $value *= static::MONTHS_PER_QUARTER * 30 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
100 | - |
|
101 | - break; |
|
102 | - |
|
103 | - // @call addRealUnit |
|
104 | - case 'year': |
|
105 | - $value *= 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
106 | - |
|
107 | - break; |
|
108 | - |
|
109 | - // @call addRealUnit |
|
110 | - case 'decade': |
|
111 | - $value *= static::YEARS_PER_DECADE * 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
112 | - |
|
113 | - break; |
|
114 | - |
|
115 | - // @call addRealUnit |
|
116 | - case 'century': |
|
117 | - $value *= static::YEARS_PER_CENTURY * 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
118 | - |
|
119 | - break; |
|
120 | - |
|
121 | - // @call addRealUnit |
|
122 | - case 'millennium': |
|
123 | - $value *= static::YEARS_PER_MILLENNIUM * 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
124 | - |
|
125 | - break; |
|
126 | - |
|
127 | - default: |
|
128 | - if ($this->localStrictModeEnabled ?? static::isStrictModeEnabled()) { |
|
129 | - throw new UnitException("Invalid unit for real timestamp add/sub: '$unit'"); |
|
130 | - } |
|
131 | - |
|
132 | - return $this; |
|
133 | - } |
|
134 | - |
|
135 | - /* @var CarbonInterface $this */ |
|
136 | - return $this->setTimestamp((int) ($this->getTimestamp() + $value)); |
|
137 | - } |
|
138 | - |
|
139 | - public function subRealUnit($unit, $value = 1) |
|
140 | - { |
|
141 | - return $this->addRealUnit($unit, -$value); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Returns true if a property can be changed via setter. |
|
146 | - * |
|
147 | - * @param string $unit |
|
148 | - * |
|
149 | - * @return bool |
|
150 | - */ |
|
151 | - public static function isModifiableUnit($unit) |
|
152 | - { |
|
153 | - static $modifiableUnits = [ |
|
154 | - // @call addUnit |
|
155 | - 'millennium', |
|
156 | - // @call addUnit |
|
157 | - 'century', |
|
158 | - // @call addUnit |
|
159 | - 'decade', |
|
160 | - // @call addUnit |
|
161 | - 'quarter', |
|
162 | - // @call addUnit |
|
163 | - 'week', |
|
164 | - // @call addUnit |
|
165 | - 'weekday', |
|
166 | - ]; |
|
167 | - |
|
168 | - return \in_array($unit, $modifiableUnits, true) || \in_array($unit, static::$units, true); |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Call native PHP DateTime/DateTimeImmutable add() method. |
|
173 | - * |
|
174 | - * @param DateInterval $interval |
|
175 | - * |
|
176 | - * @return static |
|
177 | - */ |
|
178 | - public function rawAdd(DateInterval $interval) |
|
179 | - { |
|
180 | - return parent::add($interval); |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Add given units or interval to the current instance. |
|
185 | - * |
|
186 | - * @example $date->add('hour', 3) |
|
187 | - * @example $date->add(15, 'days') |
|
188 | - * @example $date->add(CarbonInterval::days(4)) |
|
189 | - * |
|
190 | - * @param string|DateInterval|Closure|CarbonConverterInterface $unit |
|
191 | - * @param int $value |
|
192 | - * @param bool|null $overflow |
|
193 | - * |
|
194 | - * @return static |
|
195 | - */ |
|
196 | - #[ReturnTypeWillChange] |
|
197 | - public function add($unit, $value = 1, $overflow = null) |
|
198 | - { |
|
199 | - if (\is_string($unit) && \func_num_args() === 1) { |
|
200 | - $unit = CarbonInterval::make($unit); |
|
201 | - } |
|
202 | - |
|
203 | - if ($unit instanceof CarbonConverterInterface) { |
|
204 | - return $this->resolveCarbon($unit->convertDate($this, false)); |
|
205 | - } |
|
206 | - |
|
207 | - if ($unit instanceof Closure) { |
|
208 | - return $this->resolveCarbon($unit($this, false)); |
|
209 | - } |
|
210 | - |
|
211 | - if ($unit instanceof DateInterval) { |
|
212 | - return parent::add($unit); |
|
213 | - } |
|
214 | - |
|
215 | - if (is_numeric($unit)) { |
|
216 | - [$value, $unit] = [$unit, $value]; |
|
217 | - } |
|
218 | - |
|
219 | - return $this->addUnit($unit, $value, $overflow); |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Add given units to the current instance. |
|
224 | - * |
|
225 | - * @param string $unit |
|
226 | - * @param int $value |
|
227 | - * @param bool|null $overflow |
|
228 | - * |
|
229 | - * @return static |
|
230 | - */ |
|
231 | - public function addUnit($unit, $value = 1, $overflow = null) |
|
232 | - { |
|
233 | - $date = $this; |
|
234 | - |
|
235 | - if (!is_numeric($value) || !(float) $value) { |
|
236 | - return $date->isMutable() ? $date : $date->avoidMutation(); |
|
237 | - } |
|
238 | - |
|
239 | - $unit = self::singularUnit($unit); |
|
240 | - $metaUnits = [ |
|
241 | - 'millennium' => [static::YEARS_PER_MILLENNIUM, 'year'], |
|
242 | - 'century' => [static::YEARS_PER_CENTURY, 'year'], |
|
243 | - 'decade' => [static::YEARS_PER_DECADE, 'year'], |
|
244 | - 'quarter' => [static::MONTHS_PER_QUARTER, 'month'], |
|
245 | - ]; |
|
246 | - |
|
247 | - if (isset($metaUnits[$unit])) { |
|
248 | - [$factor, $unit] = $metaUnits[$unit]; |
|
249 | - $value *= $factor; |
|
250 | - } |
|
251 | - |
|
252 | - if ($unit === 'weekday') { |
|
253 | - $weekendDays = static::getWeekendDays(); |
|
254 | - |
|
255 | - if ($weekendDays !== [static::SATURDAY, static::SUNDAY]) { |
|
256 | - $absoluteValue = abs($value); |
|
257 | - $sign = $value / max(1, $absoluteValue); |
|
258 | - $weekDaysCount = 7 - min(6, \count(array_unique($weekendDays))); |
|
259 | - $weeks = floor($absoluteValue / $weekDaysCount); |
|
260 | - |
|
261 | - for ($diff = $absoluteValue % $weekDaysCount; $diff; $diff--) { |
|
262 | - /** @var static $date */ |
|
263 | - $date = $date->addDays($sign); |
|
264 | - |
|
265 | - while (\in_array($date->dayOfWeek, $weekendDays, true)) { |
|
266 | - $date = $date->addDays($sign); |
|
267 | - } |
|
268 | - } |
|
269 | - |
|
270 | - $value = $weeks * $sign; |
|
271 | - $unit = 'week'; |
|
272 | - } |
|
273 | - |
|
274 | - $timeString = $date->toTimeString(); |
|
275 | - } elseif ($canOverflow = (\in_array($unit, [ |
|
276 | - 'month', |
|
277 | - 'year', |
|
278 | - ]) && ($overflow === false || ( |
|
279 | - $overflow === null && |
|
280 | - ($ucUnit = ucfirst($unit).'s') && |
|
281 | - !($this->{'local'.$ucUnit.'Overflow'} ?? static::{'shouldOverflow'.$ucUnit}()) |
|
282 | - )))) { |
|
283 | - $day = $date->day; |
|
284 | - } |
|
285 | - |
|
286 | - $value = (int) $value; |
|
287 | - |
|
288 | - if ($unit === 'milli' || $unit === 'millisecond') { |
|
289 | - $unit = 'microsecond'; |
|
290 | - $value *= static::MICROSECONDS_PER_MILLISECOND; |
|
291 | - } |
|
292 | - |
|
293 | - // Work-around for bug https://bugs.php.net/bug.php?id=75642 |
|
294 | - if ($unit === 'micro' || $unit === 'microsecond') { |
|
295 | - $microseconds = $this->micro + $value; |
|
296 | - $second = (int) floor($microseconds / static::MICROSECONDS_PER_SECOND); |
|
297 | - $microseconds %= static::MICROSECONDS_PER_SECOND; |
|
298 | - if ($microseconds < 0) { |
|
299 | - $microseconds += static::MICROSECONDS_PER_SECOND; |
|
300 | - } |
|
301 | - $date = $date->microseconds($microseconds); |
|
302 | - $unit = 'second'; |
|
303 | - $value = $second; |
|
304 | - } |
|
305 | - $date = $date->modify("$value $unit"); |
|
306 | - |
|
307 | - if (isset($timeString)) { |
|
308 | - $date = $date->setTimeFromTimeString($timeString); |
|
309 | - } elseif (isset($canOverflow, $day) && $canOverflow && $day !== $date->day) { |
|
310 | - $date = $date->modify('last day of previous month'); |
|
311 | - } |
|
312 | - |
|
313 | - if (!$date) { |
|
314 | - throw new UnitException('Unable to add unit '.var_export(\func_get_args(), true)); |
|
315 | - } |
|
316 | - |
|
317 | - return $date; |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Subtract given units to the current instance. |
|
322 | - * |
|
323 | - * @param string $unit |
|
324 | - * @param int $value |
|
325 | - * @param bool|null $overflow |
|
326 | - * |
|
327 | - * @return static |
|
328 | - */ |
|
329 | - public function subUnit($unit, $value = 1, $overflow = null) |
|
330 | - { |
|
331 | - return $this->addUnit($unit, -$value, $overflow); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Call native PHP DateTime/DateTimeImmutable sub() method. |
|
336 | - * |
|
337 | - * @param DateInterval $interval |
|
338 | - * |
|
339 | - * @return static |
|
340 | - */ |
|
341 | - public function rawSub(DateInterval $interval) |
|
342 | - { |
|
343 | - return parent::sub($interval); |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Subtract given units or interval to the current instance. |
|
348 | - * |
|
349 | - * @example $date->sub('hour', 3) |
|
350 | - * @example $date->sub(15, 'days') |
|
351 | - * @example $date->sub(CarbonInterval::days(4)) |
|
352 | - * |
|
353 | - * @param string|DateInterval|Closure|CarbonConverterInterface $unit |
|
354 | - * @param int $value |
|
355 | - * @param bool|null $overflow |
|
356 | - * |
|
357 | - * @return static |
|
358 | - */ |
|
359 | - #[ReturnTypeWillChange] |
|
360 | - public function sub($unit, $value = 1, $overflow = null) |
|
361 | - { |
|
362 | - if (\is_string($unit) && \func_num_args() === 1) { |
|
363 | - $unit = CarbonInterval::make($unit); |
|
364 | - } |
|
365 | - |
|
366 | - if ($unit instanceof CarbonConverterInterface) { |
|
367 | - return $this->resolveCarbon($unit->convertDate($this, true)); |
|
368 | - } |
|
369 | - |
|
370 | - if ($unit instanceof Closure) { |
|
371 | - return $this->resolveCarbon($unit($this, true)); |
|
372 | - } |
|
373 | - |
|
374 | - if ($unit instanceof DateInterval) { |
|
375 | - return parent::sub($unit); |
|
376 | - } |
|
377 | - |
|
378 | - if (is_numeric($unit)) { |
|
379 | - [$value, $unit] = [$unit, $value]; |
|
380 | - } |
|
381 | - |
|
382 | - return $this->addUnit($unit, -(float) $value, $overflow); |
|
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Subtract given units or interval to the current instance. |
|
387 | - * |
|
388 | - * @see sub() |
|
389 | - * |
|
390 | - * @param string|DateInterval $unit |
|
391 | - * @param int $value |
|
392 | - * @param bool|null $overflow |
|
393 | - * |
|
394 | - * @return static |
|
395 | - */ |
|
396 | - public function subtract($unit, $value = 1, $overflow = null) |
|
397 | - { |
|
398 | - if (\is_string($unit) && \func_num_args() === 1) { |
|
399 | - $unit = CarbonInterval::make($unit); |
|
400 | - } |
|
401 | - |
|
402 | - return $this->sub($unit, $value, $overflow); |
|
403 | - } |
|
44 | + // @call addRealUnit |
|
45 | + case 'microsecond': |
|
46 | + /* @var CarbonInterface $this */ |
|
47 | + $diff = $this->microsecond + $value; |
|
48 | + $time = $this->getTimestamp(); |
|
49 | + $seconds = (int) floor($diff / static::MICROSECONDS_PER_SECOND); |
|
50 | + $time += $seconds; |
|
51 | + $diff -= $seconds * static::MICROSECONDS_PER_SECOND; |
|
52 | + $microtime = str_pad((string) $diff, 6, '0', STR_PAD_LEFT); |
|
53 | + $tz = $this->tz; |
|
54 | + |
|
55 | + return $this->tz('UTC')->modify("@$time.$microtime")->tz($tz); |
|
56 | + |
|
57 | + // @call addRealUnit |
|
58 | + case 'milli': |
|
59 | + // @call addRealUnit |
|
60 | + case 'millisecond': |
|
61 | + return $this->addRealUnit('microsecond', $value * static::MICROSECONDS_PER_MILLISECOND); |
|
62 | + |
|
63 | + // @call addRealUnit |
|
64 | + case 'second': |
|
65 | + break; |
|
66 | + |
|
67 | + // @call addRealUnit |
|
68 | + case 'minute': |
|
69 | + $value *= static::SECONDS_PER_MINUTE; |
|
70 | + |
|
71 | + break; |
|
72 | + |
|
73 | + // @call addRealUnit |
|
74 | + case 'hour': |
|
75 | + $value *= static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
76 | + |
|
77 | + break; |
|
78 | + |
|
79 | + // @call addRealUnit |
|
80 | + case 'day': |
|
81 | + $value *= static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
82 | + |
|
83 | + break; |
|
84 | + |
|
85 | + // @call addRealUnit |
|
86 | + case 'week': |
|
87 | + $value *= static::DAYS_PER_WEEK * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
88 | + |
|
89 | + break; |
|
90 | + |
|
91 | + // @call addRealUnit |
|
92 | + case 'month': |
|
93 | + $value *= 30 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
94 | + |
|
95 | + break; |
|
96 | + |
|
97 | + // @call addRealUnit |
|
98 | + case 'quarter': |
|
99 | + $value *= static::MONTHS_PER_QUARTER * 30 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
100 | + |
|
101 | + break; |
|
102 | + |
|
103 | + // @call addRealUnit |
|
104 | + case 'year': |
|
105 | + $value *= 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
106 | + |
|
107 | + break; |
|
108 | + |
|
109 | + // @call addRealUnit |
|
110 | + case 'decade': |
|
111 | + $value *= static::YEARS_PER_DECADE * 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
112 | + |
|
113 | + break; |
|
114 | + |
|
115 | + // @call addRealUnit |
|
116 | + case 'century': |
|
117 | + $value *= static::YEARS_PER_CENTURY * 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
118 | + |
|
119 | + break; |
|
120 | + |
|
121 | + // @call addRealUnit |
|
122 | + case 'millennium': |
|
123 | + $value *= static::YEARS_PER_MILLENNIUM * 365 * static::HOURS_PER_DAY * static::MINUTES_PER_HOUR * static::SECONDS_PER_MINUTE; |
|
124 | + |
|
125 | + break; |
|
126 | + |
|
127 | + default: |
|
128 | + if ($this->localStrictModeEnabled ?? static::isStrictModeEnabled()) { |
|
129 | + throw new UnitException("Invalid unit for real timestamp add/sub: '$unit'"); |
|
130 | + } |
|
131 | + |
|
132 | + return $this; |
|
133 | + } |
|
134 | + |
|
135 | + /* @var CarbonInterface $this */ |
|
136 | + return $this->setTimestamp((int) ($this->getTimestamp() + $value)); |
|
137 | + } |
|
138 | + |
|
139 | + public function subRealUnit($unit, $value = 1) |
|
140 | + { |
|
141 | + return $this->addRealUnit($unit, -$value); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Returns true if a property can be changed via setter. |
|
146 | + * |
|
147 | + * @param string $unit |
|
148 | + * |
|
149 | + * @return bool |
|
150 | + */ |
|
151 | + public static function isModifiableUnit($unit) |
|
152 | + { |
|
153 | + static $modifiableUnits = [ |
|
154 | + // @call addUnit |
|
155 | + 'millennium', |
|
156 | + // @call addUnit |
|
157 | + 'century', |
|
158 | + // @call addUnit |
|
159 | + 'decade', |
|
160 | + // @call addUnit |
|
161 | + 'quarter', |
|
162 | + // @call addUnit |
|
163 | + 'week', |
|
164 | + // @call addUnit |
|
165 | + 'weekday', |
|
166 | + ]; |
|
167 | + |
|
168 | + return \in_array($unit, $modifiableUnits, true) || \in_array($unit, static::$units, true); |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Call native PHP DateTime/DateTimeImmutable add() method. |
|
173 | + * |
|
174 | + * @param DateInterval $interval |
|
175 | + * |
|
176 | + * @return static |
|
177 | + */ |
|
178 | + public function rawAdd(DateInterval $interval) |
|
179 | + { |
|
180 | + return parent::add($interval); |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Add given units or interval to the current instance. |
|
185 | + * |
|
186 | + * @example $date->add('hour', 3) |
|
187 | + * @example $date->add(15, 'days') |
|
188 | + * @example $date->add(CarbonInterval::days(4)) |
|
189 | + * |
|
190 | + * @param string|DateInterval|Closure|CarbonConverterInterface $unit |
|
191 | + * @param int $value |
|
192 | + * @param bool|null $overflow |
|
193 | + * |
|
194 | + * @return static |
|
195 | + */ |
|
196 | + #[ReturnTypeWillChange] |
|
197 | + public function add($unit, $value = 1, $overflow = null) |
|
198 | + { |
|
199 | + if (\is_string($unit) && \func_num_args() === 1) { |
|
200 | + $unit = CarbonInterval::make($unit); |
|
201 | + } |
|
202 | + |
|
203 | + if ($unit instanceof CarbonConverterInterface) { |
|
204 | + return $this->resolveCarbon($unit->convertDate($this, false)); |
|
205 | + } |
|
206 | + |
|
207 | + if ($unit instanceof Closure) { |
|
208 | + return $this->resolveCarbon($unit($this, false)); |
|
209 | + } |
|
210 | + |
|
211 | + if ($unit instanceof DateInterval) { |
|
212 | + return parent::add($unit); |
|
213 | + } |
|
214 | + |
|
215 | + if (is_numeric($unit)) { |
|
216 | + [$value, $unit] = [$unit, $value]; |
|
217 | + } |
|
218 | + |
|
219 | + return $this->addUnit($unit, $value, $overflow); |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Add given units to the current instance. |
|
224 | + * |
|
225 | + * @param string $unit |
|
226 | + * @param int $value |
|
227 | + * @param bool|null $overflow |
|
228 | + * |
|
229 | + * @return static |
|
230 | + */ |
|
231 | + public function addUnit($unit, $value = 1, $overflow = null) |
|
232 | + { |
|
233 | + $date = $this; |
|
234 | + |
|
235 | + if (!is_numeric($value) || !(float) $value) { |
|
236 | + return $date->isMutable() ? $date : $date->avoidMutation(); |
|
237 | + } |
|
238 | + |
|
239 | + $unit = self::singularUnit($unit); |
|
240 | + $metaUnits = [ |
|
241 | + 'millennium' => [static::YEARS_PER_MILLENNIUM, 'year'], |
|
242 | + 'century' => [static::YEARS_PER_CENTURY, 'year'], |
|
243 | + 'decade' => [static::YEARS_PER_DECADE, 'year'], |
|
244 | + 'quarter' => [static::MONTHS_PER_QUARTER, 'month'], |
|
245 | + ]; |
|
246 | + |
|
247 | + if (isset($metaUnits[$unit])) { |
|
248 | + [$factor, $unit] = $metaUnits[$unit]; |
|
249 | + $value *= $factor; |
|
250 | + } |
|
251 | + |
|
252 | + if ($unit === 'weekday') { |
|
253 | + $weekendDays = static::getWeekendDays(); |
|
254 | + |
|
255 | + if ($weekendDays !== [static::SATURDAY, static::SUNDAY]) { |
|
256 | + $absoluteValue = abs($value); |
|
257 | + $sign = $value / max(1, $absoluteValue); |
|
258 | + $weekDaysCount = 7 - min(6, \count(array_unique($weekendDays))); |
|
259 | + $weeks = floor($absoluteValue / $weekDaysCount); |
|
260 | + |
|
261 | + for ($diff = $absoluteValue % $weekDaysCount; $diff; $diff--) { |
|
262 | + /** @var static $date */ |
|
263 | + $date = $date->addDays($sign); |
|
264 | + |
|
265 | + while (\in_array($date->dayOfWeek, $weekendDays, true)) { |
|
266 | + $date = $date->addDays($sign); |
|
267 | + } |
|
268 | + } |
|
269 | + |
|
270 | + $value = $weeks * $sign; |
|
271 | + $unit = 'week'; |
|
272 | + } |
|
273 | + |
|
274 | + $timeString = $date->toTimeString(); |
|
275 | + } elseif ($canOverflow = (\in_array($unit, [ |
|
276 | + 'month', |
|
277 | + 'year', |
|
278 | + ]) && ($overflow === false || ( |
|
279 | + $overflow === null && |
|
280 | + ($ucUnit = ucfirst($unit).'s') && |
|
281 | + !($this->{'local'.$ucUnit.'Overflow'} ?? static::{'shouldOverflow'.$ucUnit}()) |
|
282 | + )))) { |
|
283 | + $day = $date->day; |
|
284 | + } |
|
285 | + |
|
286 | + $value = (int) $value; |
|
287 | + |
|
288 | + if ($unit === 'milli' || $unit === 'millisecond') { |
|
289 | + $unit = 'microsecond'; |
|
290 | + $value *= static::MICROSECONDS_PER_MILLISECOND; |
|
291 | + } |
|
292 | + |
|
293 | + // Work-around for bug https://bugs.php.net/bug.php?id=75642 |
|
294 | + if ($unit === 'micro' || $unit === 'microsecond') { |
|
295 | + $microseconds = $this->micro + $value; |
|
296 | + $second = (int) floor($microseconds / static::MICROSECONDS_PER_SECOND); |
|
297 | + $microseconds %= static::MICROSECONDS_PER_SECOND; |
|
298 | + if ($microseconds < 0) { |
|
299 | + $microseconds += static::MICROSECONDS_PER_SECOND; |
|
300 | + } |
|
301 | + $date = $date->microseconds($microseconds); |
|
302 | + $unit = 'second'; |
|
303 | + $value = $second; |
|
304 | + } |
|
305 | + $date = $date->modify("$value $unit"); |
|
306 | + |
|
307 | + if (isset($timeString)) { |
|
308 | + $date = $date->setTimeFromTimeString($timeString); |
|
309 | + } elseif (isset($canOverflow, $day) && $canOverflow && $day !== $date->day) { |
|
310 | + $date = $date->modify('last day of previous month'); |
|
311 | + } |
|
312 | + |
|
313 | + if (!$date) { |
|
314 | + throw new UnitException('Unable to add unit '.var_export(\func_get_args(), true)); |
|
315 | + } |
|
316 | + |
|
317 | + return $date; |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Subtract given units to the current instance. |
|
322 | + * |
|
323 | + * @param string $unit |
|
324 | + * @param int $value |
|
325 | + * @param bool|null $overflow |
|
326 | + * |
|
327 | + * @return static |
|
328 | + */ |
|
329 | + public function subUnit($unit, $value = 1, $overflow = null) |
|
330 | + { |
|
331 | + return $this->addUnit($unit, -$value, $overflow); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Call native PHP DateTime/DateTimeImmutable sub() method. |
|
336 | + * |
|
337 | + * @param DateInterval $interval |
|
338 | + * |
|
339 | + * @return static |
|
340 | + */ |
|
341 | + public function rawSub(DateInterval $interval) |
|
342 | + { |
|
343 | + return parent::sub($interval); |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Subtract given units or interval to the current instance. |
|
348 | + * |
|
349 | + * @example $date->sub('hour', 3) |
|
350 | + * @example $date->sub(15, 'days') |
|
351 | + * @example $date->sub(CarbonInterval::days(4)) |
|
352 | + * |
|
353 | + * @param string|DateInterval|Closure|CarbonConverterInterface $unit |
|
354 | + * @param int $value |
|
355 | + * @param bool|null $overflow |
|
356 | + * |
|
357 | + * @return static |
|
358 | + */ |
|
359 | + #[ReturnTypeWillChange] |
|
360 | + public function sub($unit, $value = 1, $overflow = null) |
|
361 | + { |
|
362 | + if (\is_string($unit) && \func_num_args() === 1) { |
|
363 | + $unit = CarbonInterval::make($unit); |
|
364 | + } |
|
365 | + |
|
366 | + if ($unit instanceof CarbonConverterInterface) { |
|
367 | + return $this->resolveCarbon($unit->convertDate($this, true)); |
|
368 | + } |
|
369 | + |
|
370 | + if ($unit instanceof Closure) { |
|
371 | + return $this->resolveCarbon($unit($this, true)); |
|
372 | + } |
|
373 | + |
|
374 | + if ($unit instanceof DateInterval) { |
|
375 | + return parent::sub($unit); |
|
376 | + } |
|
377 | + |
|
378 | + if (is_numeric($unit)) { |
|
379 | + [$value, $unit] = [$unit, $value]; |
|
380 | + } |
|
381 | + |
|
382 | + return $this->addUnit($unit, -(float) $value, $overflow); |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Subtract given units or interval to the current instance. |
|
387 | + * |
|
388 | + * @see sub() |
|
389 | + * |
|
390 | + * @param string|DateInterval $unit |
|
391 | + * @param int $value |
|
392 | + * @param bool|null $overflow |
|
393 | + * |
|
394 | + * @return static |
|
395 | + */ |
|
396 | + public function subtract($unit, $value = 1, $overflow = null) |
|
397 | + { |
|
398 | + if (\is_string($unit) && \func_num_args() === 1) { |
|
399 | + $unit = CarbonInterval::make($unit); |
|
400 | + } |
|
401 | + |
|
402 | + return $this->sub($unit, $value, $overflow); |
|
403 | + } |
|
404 | 404 | } |
@@ -13,10 +13,10 @@ |
||
13 | 13 | |
14 | 14 | trait ObjectInitialisation |
15 | 15 | { |
16 | - /** |
|
17 | - * True when parent::__construct has been called. |
|
18 | - * |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - protected $constructedObjectId; |
|
16 | + /** |
|
17 | + * True when parent::__construct has been called. |
|
18 | + * |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + protected $constructedObjectId; |
|
22 | 22 | } |
@@ -21,452 +21,452 @@ |
||
21 | 21 | */ |
22 | 22 | trait Modifiers |
23 | 23 | { |
24 | - /** |
|
25 | - * Midday/noon hour. |
|
26 | - * |
|
27 | - * @var int |
|
28 | - */ |
|
29 | - protected static $midDayAt = 12; |
|
30 | - |
|
31 | - /** |
|
32 | - * get midday/noon hour |
|
33 | - * |
|
34 | - * @return int |
|
35 | - */ |
|
36 | - public static function getMidDayAt() |
|
37 | - { |
|
38 | - return static::$midDayAt; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
43 | - * You should rather consider mid-day is always 12pm, then if you need to test if it's an other |
|
44 | - * hour, test it explicitly: |
|
45 | - * $date->format('G') == 13 |
|
46 | - * or to set explicitly to a given hour: |
|
47 | - * $date->setTime(13, 0, 0, 0) |
|
48 | - * |
|
49 | - * Set midday/noon hour |
|
50 | - * |
|
51 | - * @param int $hour midday hour |
|
52 | - * |
|
53 | - * @return void |
|
54 | - */ |
|
55 | - public static function setMidDayAt($hour) |
|
56 | - { |
|
57 | - static::$midDayAt = $hour; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Modify to midday, default to self::$midDayAt |
|
62 | - * |
|
63 | - * @return static |
|
64 | - */ |
|
65 | - public function midDay() |
|
66 | - { |
|
67 | - return $this->setTime(static::$midDayAt, 0, 0, 0); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Modify to the next occurrence of a given modifier such as a day of |
|
72 | - * the week. If no modifier is provided, modify to the next occurrence |
|
73 | - * of the current day of the week. Use the supplied constants |
|
74 | - * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
75 | - * |
|
76 | - * @param string|int|null $modifier |
|
77 | - * |
|
78 | - * @return static |
|
79 | - */ |
|
80 | - public function next($modifier = null) |
|
81 | - { |
|
82 | - if ($modifier === null) { |
|
83 | - $modifier = $this->dayOfWeek; |
|
84 | - } |
|
85 | - |
|
86 | - return $this->change( |
|
87 | - 'next '.(\is_string($modifier) ? $modifier : static::$days[$modifier]) |
|
88 | - ); |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Go forward or backward to the next week- or weekend-day. |
|
93 | - * |
|
94 | - * @param bool $weekday |
|
95 | - * @param bool $forward |
|
96 | - * |
|
97 | - * @return static |
|
98 | - */ |
|
99 | - private function nextOrPreviousDay($weekday = true, $forward = true) |
|
100 | - { |
|
101 | - /** @var CarbonInterface $date */ |
|
102 | - $date = $this; |
|
103 | - $step = $forward ? 1 : -1; |
|
104 | - |
|
105 | - do { |
|
106 | - $date = $date->addDays($step); |
|
107 | - } while ($weekday ? $date->isWeekend() : $date->isWeekday()); |
|
108 | - |
|
109 | - return $date; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Go forward to the next weekday. |
|
114 | - * |
|
115 | - * @return static |
|
116 | - */ |
|
117 | - public function nextWeekday() |
|
118 | - { |
|
119 | - return $this->nextOrPreviousDay(); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Go backward to the previous weekday. |
|
124 | - * |
|
125 | - * @return static |
|
126 | - */ |
|
127 | - public function previousWeekday() |
|
128 | - { |
|
129 | - return $this->nextOrPreviousDay(true, false); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Go forward to the next weekend day. |
|
134 | - * |
|
135 | - * @return static |
|
136 | - */ |
|
137 | - public function nextWeekendDay() |
|
138 | - { |
|
139 | - return $this->nextOrPreviousDay(false); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Go backward to the previous weekend day. |
|
144 | - * |
|
145 | - * @return static |
|
146 | - */ |
|
147 | - public function previousWeekendDay() |
|
148 | - { |
|
149 | - return $this->nextOrPreviousDay(false, false); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Modify to the previous occurrence of a given modifier such as a day of |
|
154 | - * the week. If no dayOfWeek is provided, modify to the previous occurrence |
|
155 | - * of the current day of the week. Use the supplied constants |
|
156 | - * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
157 | - * |
|
158 | - * @param string|int|null $modifier |
|
159 | - * |
|
160 | - * @return static |
|
161 | - */ |
|
162 | - public function previous($modifier = null) |
|
163 | - { |
|
164 | - if ($modifier === null) { |
|
165 | - $modifier = $this->dayOfWeek; |
|
166 | - } |
|
167 | - |
|
168 | - return $this->change( |
|
169 | - 'last '.(\is_string($modifier) ? $modifier : static::$days[$modifier]) |
|
170 | - ); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Modify to the first occurrence of a given day of the week |
|
175 | - * in the current month. If no dayOfWeek is provided, modify to the |
|
176 | - * first day of the current month. Use the supplied constants |
|
177 | - * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
178 | - * |
|
179 | - * @param int|null $dayOfWeek |
|
180 | - * |
|
181 | - * @return static |
|
182 | - */ |
|
183 | - public function firstOfMonth($dayOfWeek = null) |
|
184 | - { |
|
185 | - $date = $this->startOfDay(); |
|
186 | - |
|
187 | - if ($dayOfWeek === null) { |
|
188 | - return $date->day(1); |
|
189 | - } |
|
190 | - |
|
191 | - return $date->modify('first '.static::$days[$dayOfWeek].' of '.$date->rawFormat('F').' '.$date->year); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Modify to the last occurrence of a given day of the week |
|
196 | - * in the current month. If no dayOfWeek is provided, modify to the |
|
197 | - * last day of the current month. Use the supplied constants |
|
198 | - * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
199 | - * |
|
200 | - * @param int|null $dayOfWeek |
|
201 | - * |
|
202 | - * @return static |
|
203 | - */ |
|
204 | - public function lastOfMonth($dayOfWeek = null) |
|
205 | - { |
|
206 | - $date = $this->startOfDay(); |
|
207 | - |
|
208 | - if ($dayOfWeek === null) { |
|
209 | - return $date->day($date->daysInMonth); |
|
210 | - } |
|
211 | - |
|
212 | - return $date->modify('last '.static::$days[$dayOfWeek].' of '.$date->rawFormat('F').' '.$date->year); |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Modify to the given occurrence of a given day of the week |
|
217 | - * in the current month. If the calculated occurrence is outside the scope |
|
218 | - * of the current month, then return false and no modifications are made. |
|
219 | - * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
220 | - * |
|
221 | - * @param int $nth |
|
222 | - * @param int $dayOfWeek |
|
223 | - * |
|
224 | - * @return mixed |
|
225 | - */ |
|
226 | - public function nthOfMonth($nth, $dayOfWeek) |
|
227 | - { |
|
228 | - $date = $this->avoidMutation()->firstOfMonth(); |
|
229 | - $check = $date->rawFormat('Y-m'); |
|
230 | - $date = $date->modify('+'.$nth.' '.static::$days[$dayOfWeek]); |
|
231 | - |
|
232 | - return $date->rawFormat('Y-m') === $check ? $this->modify((string) $date) : false; |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * Modify to the first occurrence of a given day of the week |
|
237 | - * in the current quarter. If no dayOfWeek is provided, modify to the |
|
238 | - * first day of the current quarter. Use the supplied constants |
|
239 | - * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
240 | - * |
|
241 | - * @param int|null $dayOfWeek day of the week default null |
|
242 | - * |
|
243 | - * @return static |
|
244 | - */ |
|
245 | - public function firstOfQuarter($dayOfWeek = null) |
|
246 | - { |
|
247 | - return $this->setDate($this->year, $this->quarter * static::MONTHS_PER_QUARTER - 2, 1)->firstOfMonth($dayOfWeek); |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Modify to the last occurrence of a given day of the week |
|
252 | - * in the current quarter. If no dayOfWeek is provided, modify to the |
|
253 | - * last day of the current quarter. Use the supplied constants |
|
254 | - * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
255 | - * |
|
256 | - * @param int|null $dayOfWeek day of the week default null |
|
257 | - * |
|
258 | - * @return static |
|
259 | - */ |
|
260 | - public function lastOfQuarter($dayOfWeek = null) |
|
261 | - { |
|
262 | - return $this->setDate($this->year, $this->quarter * static::MONTHS_PER_QUARTER, 1)->lastOfMonth($dayOfWeek); |
|
263 | - } |
|
264 | - |
|
265 | - /** |
|
266 | - * Modify to the given occurrence of a given day of the week |
|
267 | - * in the current quarter. If the calculated occurrence is outside the scope |
|
268 | - * of the current quarter, then return false and no modifications are made. |
|
269 | - * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
270 | - * |
|
271 | - * @param int $nth |
|
272 | - * @param int $dayOfWeek |
|
273 | - * |
|
274 | - * @return mixed |
|
275 | - */ |
|
276 | - public function nthOfQuarter($nth, $dayOfWeek) |
|
277 | - { |
|
278 | - $date = $this->avoidMutation()->day(1)->month($this->quarter * static::MONTHS_PER_QUARTER); |
|
279 | - $lastMonth = $date->month; |
|
280 | - $year = $date->year; |
|
281 | - $date = $date->firstOfQuarter()->modify('+'.$nth.' '.static::$days[$dayOfWeek]); |
|
282 | - |
|
283 | - return ($lastMonth < $date->month || $year !== $date->year) ? false : $this->modify((string) $date); |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * Modify to the first occurrence of a given day of the week |
|
288 | - * in the current year. If no dayOfWeek is provided, modify to the |
|
289 | - * first day of the current year. Use the supplied constants |
|
290 | - * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
291 | - * |
|
292 | - * @param int|null $dayOfWeek day of the week default null |
|
293 | - * |
|
294 | - * @return static |
|
295 | - */ |
|
296 | - public function firstOfYear($dayOfWeek = null) |
|
297 | - { |
|
298 | - return $this->month(1)->firstOfMonth($dayOfWeek); |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Modify to the last occurrence of a given day of the week |
|
303 | - * in the current year. If no dayOfWeek is provided, modify to the |
|
304 | - * last day of the current year. Use the supplied constants |
|
305 | - * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
306 | - * |
|
307 | - * @param int|null $dayOfWeek day of the week default null |
|
308 | - * |
|
309 | - * @return static |
|
310 | - */ |
|
311 | - public function lastOfYear($dayOfWeek = null) |
|
312 | - { |
|
313 | - return $this->month(static::MONTHS_PER_YEAR)->lastOfMonth($dayOfWeek); |
|
314 | - } |
|
315 | - |
|
316 | - /** |
|
317 | - * Modify to the given occurrence of a given day of the week |
|
318 | - * in the current year. If the calculated occurrence is outside the scope |
|
319 | - * of the current year, then return false and no modifications are made. |
|
320 | - * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
321 | - * |
|
322 | - * @param int $nth |
|
323 | - * @param int $dayOfWeek |
|
324 | - * |
|
325 | - * @return mixed |
|
326 | - */ |
|
327 | - public function nthOfYear($nth, $dayOfWeek) |
|
328 | - { |
|
329 | - $date = $this->avoidMutation()->firstOfYear()->modify('+'.$nth.' '.static::$days[$dayOfWeek]); |
|
330 | - |
|
331 | - return $this->year === $date->year ? $this->modify((string) $date) : false; |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Modify the current instance to the average of a given instance (default now) and the current instance |
|
336 | - * (second-precision). |
|
337 | - * |
|
338 | - * @param \Carbon\Carbon|\DateTimeInterface|null $date |
|
339 | - * |
|
340 | - * @return static |
|
341 | - */ |
|
342 | - public function average($date = null) |
|
343 | - { |
|
344 | - return $this->addRealMicroseconds((int) ($this->diffInRealMicroseconds($this->resolveCarbon($date), false) / 2)); |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * Get the closest date from the instance (second-precision). |
|
349 | - * |
|
350 | - * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1 |
|
351 | - * @param \Carbon\Carbon|\DateTimeInterface|mixed $date2 |
|
352 | - * |
|
353 | - * @return static |
|
354 | - */ |
|
355 | - public function closest($date1, $date2) |
|
356 | - { |
|
357 | - return $this->diffInRealMicroseconds($date1) < $this->diffInRealMicroseconds($date2) ? $date1 : $date2; |
|
358 | - } |
|
359 | - |
|
360 | - /** |
|
361 | - * Get the farthest date from the instance (second-precision). |
|
362 | - * |
|
363 | - * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1 |
|
364 | - * @param \Carbon\Carbon|\DateTimeInterface|mixed $date2 |
|
365 | - * |
|
366 | - * @return static |
|
367 | - */ |
|
368 | - public function farthest($date1, $date2) |
|
369 | - { |
|
370 | - return $this->diffInRealMicroseconds($date1) > $this->diffInRealMicroseconds($date2) ? $date1 : $date2; |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Get the minimum instance between a given instance (default now) and the current instance. |
|
375 | - * |
|
376 | - * @param \Carbon\Carbon|\DateTimeInterface|mixed $date |
|
377 | - * |
|
378 | - * @return static |
|
379 | - */ |
|
380 | - public function min($date = null) |
|
381 | - { |
|
382 | - $date = $this->resolveCarbon($date); |
|
383 | - |
|
384 | - return $this->lt($date) ? $this : $date; |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Get the minimum instance between a given instance (default now) and the current instance. |
|
389 | - * |
|
390 | - * @param \Carbon\Carbon|\DateTimeInterface|mixed $date |
|
391 | - * |
|
392 | - * @see min() |
|
393 | - * |
|
394 | - * @return static |
|
395 | - */ |
|
396 | - public function minimum($date = null) |
|
397 | - { |
|
398 | - return $this->min($date); |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * Get the maximum instance between a given instance (default now) and the current instance. |
|
403 | - * |
|
404 | - * @param \Carbon\Carbon|\DateTimeInterface|mixed $date |
|
405 | - * |
|
406 | - * @return static |
|
407 | - */ |
|
408 | - public function max($date = null) |
|
409 | - { |
|
410 | - $date = $this->resolveCarbon($date); |
|
411 | - |
|
412 | - return $this->gt($date) ? $this : $date; |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Get the maximum instance between a given instance (default now) and the current instance. |
|
417 | - * |
|
418 | - * @param \Carbon\Carbon|\DateTimeInterface|mixed $date |
|
419 | - * |
|
420 | - * @see max() |
|
421 | - * |
|
422 | - * @return static |
|
423 | - */ |
|
424 | - public function maximum($date = null) |
|
425 | - { |
|
426 | - return $this->max($date); |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Calls \DateTime::modify if mutable or \DateTimeImmutable::modify else. |
|
431 | - * |
|
432 | - * @see https://php.net/manual/en/datetime.modify.php |
|
433 | - * |
|
434 | - * @return static|false |
|
435 | - */ |
|
436 | - #[ReturnTypeWillChange] |
|
437 | - public function modify($modify) |
|
438 | - { |
|
439 | - return parent::modify((string) $modify); |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * Similar to native modify() method of DateTime but can handle more grammars. |
|
444 | - * |
|
445 | - * @example |
|
446 | - * ``` |
|
447 | - * echo Carbon::now()->change('next 2pm'); |
|
448 | - * ``` |
|
449 | - * |
|
450 | - * @link https://php.net/manual/en/datetime.modify.php |
|
451 | - * |
|
452 | - * @param string $modifier |
|
453 | - * |
|
454 | - * @return static |
|
455 | - */ |
|
456 | - public function change($modifier) |
|
457 | - { |
|
458 | - return $this->modify(preg_replace_callback('/^(next|previous|last)\s+(\d{1,2}(h|am|pm|:\d{1,2}(:\d{1,2})?))$/i', function ($match) { |
|
459 | - $match[2] = str_replace('h', ':00', $match[2]); |
|
460 | - $test = $this->avoidMutation()->modify($match[2]); |
|
461 | - $method = $match[1] === 'next' ? 'lt' : 'gt'; |
|
462 | - $match[1] = $test->$method($this) ? $match[1].' day' : 'today'; |
|
463 | - |
|
464 | - return $match[1].' '.$match[2]; |
|
465 | - }, strtr(trim($modifier), [ |
|
466 | - ' at ' => ' ', |
|
467 | - 'just now' => 'now', |
|
468 | - 'after tomorrow' => 'tomorrow +1 day', |
|
469 | - 'before yesterday' => 'yesterday -1 day', |
|
470 | - ]))); |
|
471 | - } |
|
24 | + /** |
|
25 | + * Midday/noon hour. |
|
26 | + * |
|
27 | + * @var int |
|
28 | + */ |
|
29 | + protected static $midDayAt = 12; |
|
30 | + |
|
31 | + /** |
|
32 | + * get midday/noon hour |
|
33 | + * |
|
34 | + * @return int |
|
35 | + */ |
|
36 | + public static function getMidDayAt() |
|
37 | + { |
|
38 | + return static::$midDayAt; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
|
43 | + * You should rather consider mid-day is always 12pm, then if you need to test if it's an other |
|
44 | + * hour, test it explicitly: |
|
45 | + * $date->format('G') == 13 |
|
46 | + * or to set explicitly to a given hour: |
|
47 | + * $date->setTime(13, 0, 0, 0) |
|
48 | + * |
|
49 | + * Set midday/noon hour |
|
50 | + * |
|
51 | + * @param int $hour midday hour |
|
52 | + * |
|
53 | + * @return void |
|
54 | + */ |
|
55 | + public static function setMidDayAt($hour) |
|
56 | + { |
|
57 | + static::$midDayAt = $hour; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Modify to midday, default to self::$midDayAt |
|
62 | + * |
|
63 | + * @return static |
|
64 | + */ |
|
65 | + public function midDay() |
|
66 | + { |
|
67 | + return $this->setTime(static::$midDayAt, 0, 0, 0); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Modify to the next occurrence of a given modifier such as a day of |
|
72 | + * the week. If no modifier is provided, modify to the next occurrence |
|
73 | + * of the current day of the week. Use the supplied constants |
|
74 | + * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
75 | + * |
|
76 | + * @param string|int|null $modifier |
|
77 | + * |
|
78 | + * @return static |
|
79 | + */ |
|
80 | + public function next($modifier = null) |
|
81 | + { |
|
82 | + if ($modifier === null) { |
|
83 | + $modifier = $this->dayOfWeek; |
|
84 | + } |
|
85 | + |
|
86 | + return $this->change( |
|
87 | + 'next '.(\is_string($modifier) ? $modifier : static::$days[$modifier]) |
|
88 | + ); |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Go forward or backward to the next week- or weekend-day. |
|
93 | + * |
|
94 | + * @param bool $weekday |
|
95 | + * @param bool $forward |
|
96 | + * |
|
97 | + * @return static |
|
98 | + */ |
|
99 | + private function nextOrPreviousDay($weekday = true, $forward = true) |
|
100 | + { |
|
101 | + /** @var CarbonInterface $date */ |
|
102 | + $date = $this; |
|
103 | + $step = $forward ? 1 : -1; |
|
104 | + |
|
105 | + do { |
|
106 | + $date = $date->addDays($step); |
|
107 | + } while ($weekday ? $date->isWeekend() : $date->isWeekday()); |
|
108 | + |
|
109 | + return $date; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Go forward to the next weekday. |
|
114 | + * |
|
115 | + * @return static |
|
116 | + */ |
|
117 | + public function nextWeekday() |
|
118 | + { |
|
119 | + return $this->nextOrPreviousDay(); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Go backward to the previous weekday. |
|
124 | + * |
|
125 | + * @return static |
|
126 | + */ |
|
127 | + public function previousWeekday() |
|
128 | + { |
|
129 | + return $this->nextOrPreviousDay(true, false); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Go forward to the next weekend day. |
|
134 | + * |
|
135 | + * @return static |
|
136 | + */ |
|
137 | + public function nextWeekendDay() |
|
138 | + { |
|
139 | + return $this->nextOrPreviousDay(false); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Go backward to the previous weekend day. |
|
144 | + * |
|
145 | + * @return static |
|
146 | + */ |
|
147 | + public function previousWeekendDay() |
|
148 | + { |
|
149 | + return $this->nextOrPreviousDay(false, false); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Modify to the previous occurrence of a given modifier such as a day of |
|
154 | + * the week. If no dayOfWeek is provided, modify to the previous occurrence |
|
155 | + * of the current day of the week. Use the supplied constants |
|
156 | + * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
157 | + * |
|
158 | + * @param string|int|null $modifier |
|
159 | + * |
|
160 | + * @return static |
|
161 | + */ |
|
162 | + public function previous($modifier = null) |
|
163 | + { |
|
164 | + if ($modifier === null) { |
|
165 | + $modifier = $this->dayOfWeek; |
|
166 | + } |
|
167 | + |
|
168 | + return $this->change( |
|
169 | + 'last '.(\is_string($modifier) ? $modifier : static::$days[$modifier]) |
|
170 | + ); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Modify to the first occurrence of a given day of the week |
|
175 | + * in the current month. If no dayOfWeek is provided, modify to the |
|
176 | + * first day of the current month. Use the supplied constants |
|
177 | + * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
178 | + * |
|
179 | + * @param int|null $dayOfWeek |
|
180 | + * |
|
181 | + * @return static |
|
182 | + */ |
|
183 | + public function firstOfMonth($dayOfWeek = null) |
|
184 | + { |
|
185 | + $date = $this->startOfDay(); |
|
186 | + |
|
187 | + if ($dayOfWeek === null) { |
|
188 | + return $date->day(1); |
|
189 | + } |
|
190 | + |
|
191 | + return $date->modify('first '.static::$days[$dayOfWeek].' of '.$date->rawFormat('F').' '.$date->year); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Modify to the last occurrence of a given day of the week |
|
196 | + * in the current month. If no dayOfWeek is provided, modify to the |
|
197 | + * last day of the current month. Use the supplied constants |
|
198 | + * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
199 | + * |
|
200 | + * @param int|null $dayOfWeek |
|
201 | + * |
|
202 | + * @return static |
|
203 | + */ |
|
204 | + public function lastOfMonth($dayOfWeek = null) |
|
205 | + { |
|
206 | + $date = $this->startOfDay(); |
|
207 | + |
|
208 | + if ($dayOfWeek === null) { |
|
209 | + return $date->day($date->daysInMonth); |
|
210 | + } |
|
211 | + |
|
212 | + return $date->modify('last '.static::$days[$dayOfWeek].' of '.$date->rawFormat('F').' '.$date->year); |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Modify to the given occurrence of a given day of the week |
|
217 | + * in the current month. If the calculated occurrence is outside the scope |
|
218 | + * of the current month, then return false and no modifications are made. |
|
219 | + * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
220 | + * |
|
221 | + * @param int $nth |
|
222 | + * @param int $dayOfWeek |
|
223 | + * |
|
224 | + * @return mixed |
|
225 | + */ |
|
226 | + public function nthOfMonth($nth, $dayOfWeek) |
|
227 | + { |
|
228 | + $date = $this->avoidMutation()->firstOfMonth(); |
|
229 | + $check = $date->rawFormat('Y-m'); |
|
230 | + $date = $date->modify('+'.$nth.' '.static::$days[$dayOfWeek]); |
|
231 | + |
|
232 | + return $date->rawFormat('Y-m') === $check ? $this->modify((string) $date) : false; |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * Modify to the first occurrence of a given day of the week |
|
237 | + * in the current quarter. If no dayOfWeek is provided, modify to the |
|
238 | + * first day of the current quarter. Use the supplied constants |
|
239 | + * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
240 | + * |
|
241 | + * @param int|null $dayOfWeek day of the week default null |
|
242 | + * |
|
243 | + * @return static |
|
244 | + */ |
|
245 | + public function firstOfQuarter($dayOfWeek = null) |
|
246 | + { |
|
247 | + return $this->setDate($this->year, $this->quarter * static::MONTHS_PER_QUARTER - 2, 1)->firstOfMonth($dayOfWeek); |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Modify to the last occurrence of a given day of the week |
|
252 | + * in the current quarter. If no dayOfWeek is provided, modify to the |
|
253 | + * last day of the current quarter. Use the supplied constants |
|
254 | + * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
255 | + * |
|
256 | + * @param int|null $dayOfWeek day of the week default null |
|
257 | + * |
|
258 | + * @return static |
|
259 | + */ |
|
260 | + public function lastOfQuarter($dayOfWeek = null) |
|
261 | + { |
|
262 | + return $this->setDate($this->year, $this->quarter * static::MONTHS_PER_QUARTER, 1)->lastOfMonth($dayOfWeek); |
|
263 | + } |
|
264 | + |
|
265 | + /** |
|
266 | + * Modify to the given occurrence of a given day of the week |
|
267 | + * in the current quarter. If the calculated occurrence is outside the scope |
|
268 | + * of the current quarter, then return false and no modifications are made. |
|
269 | + * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
270 | + * |
|
271 | + * @param int $nth |
|
272 | + * @param int $dayOfWeek |
|
273 | + * |
|
274 | + * @return mixed |
|
275 | + */ |
|
276 | + public function nthOfQuarter($nth, $dayOfWeek) |
|
277 | + { |
|
278 | + $date = $this->avoidMutation()->day(1)->month($this->quarter * static::MONTHS_PER_QUARTER); |
|
279 | + $lastMonth = $date->month; |
|
280 | + $year = $date->year; |
|
281 | + $date = $date->firstOfQuarter()->modify('+'.$nth.' '.static::$days[$dayOfWeek]); |
|
282 | + |
|
283 | + return ($lastMonth < $date->month || $year !== $date->year) ? false : $this->modify((string) $date); |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * Modify to the first occurrence of a given day of the week |
|
288 | + * in the current year. If no dayOfWeek is provided, modify to the |
|
289 | + * first day of the current year. Use the supplied constants |
|
290 | + * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
291 | + * |
|
292 | + * @param int|null $dayOfWeek day of the week default null |
|
293 | + * |
|
294 | + * @return static |
|
295 | + */ |
|
296 | + public function firstOfYear($dayOfWeek = null) |
|
297 | + { |
|
298 | + return $this->month(1)->firstOfMonth($dayOfWeek); |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Modify to the last occurrence of a given day of the week |
|
303 | + * in the current year. If no dayOfWeek is provided, modify to the |
|
304 | + * last day of the current year. Use the supplied constants |
|
305 | + * to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
306 | + * |
|
307 | + * @param int|null $dayOfWeek day of the week default null |
|
308 | + * |
|
309 | + * @return static |
|
310 | + */ |
|
311 | + public function lastOfYear($dayOfWeek = null) |
|
312 | + { |
|
313 | + return $this->month(static::MONTHS_PER_YEAR)->lastOfMonth($dayOfWeek); |
|
314 | + } |
|
315 | + |
|
316 | + /** |
|
317 | + * Modify to the given occurrence of a given day of the week |
|
318 | + * in the current year. If the calculated occurrence is outside the scope |
|
319 | + * of the current year, then return false and no modifications are made. |
|
320 | + * Use the supplied constants to indicate the desired dayOfWeek, ex. static::MONDAY. |
|
321 | + * |
|
322 | + * @param int $nth |
|
323 | + * @param int $dayOfWeek |
|
324 | + * |
|
325 | + * @return mixed |
|
326 | + */ |
|
327 | + public function nthOfYear($nth, $dayOfWeek) |
|
328 | + { |
|
329 | + $date = $this->avoidMutation()->firstOfYear()->modify('+'.$nth.' '.static::$days[$dayOfWeek]); |
|
330 | + |
|
331 | + return $this->year === $date->year ? $this->modify((string) $date) : false; |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Modify the current instance to the average of a given instance (default now) and the current instance |
|
336 | + * (second-precision). |
|
337 | + * |
|
338 | + * @param \Carbon\Carbon|\DateTimeInterface|null $date |
|
339 | + * |
|
340 | + * @return static |
|
341 | + */ |
|
342 | + public function average($date = null) |
|
343 | + { |
|
344 | + return $this->addRealMicroseconds((int) ($this->diffInRealMicroseconds($this->resolveCarbon($date), false) / 2)); |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * Get the closest date from the instance (second-precision). |
|
349 | + * |
|
350 | + * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1 |
|
351 | + * @param \Carbon\Carbon|\DateTimeInterface|mixed $date2 |
|
352 | + * |
|
353 | + * @return static |
|
354 | + */ |
|
355 | + public function closest($date1, $date2) |
|
356 | + { |
|
357 | + return $this->diffInRealMicroseconds($date1) < $this->diffInRealMicroseconds($date2) ? $date1 : $date2; |
|
358 | + } |
|
359 | + |
|
360 | + /** |
|
361 | + * Get the farthest date from the instance (second-precision). |
|
362 | + * |
|
363 | + * @param \Carbon\Carbon|\DateTimeInterface|mixed $date1 |
|
364 | + * @param \Carbon\Carbon|\DateTimeInterface|mixed $date2 |
|
365 | + * |
|
366 | + * @return static |
|
367 | + */ |
|
368 | + public function farthest($date1, $date2) |
|
369 | + { |
|
370 | + return $this->diffInRealMicroseconds($date1) > $this->diffInRealMicroseconds($date2) ? $date1 : $date2; |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Get the minimum instance between a given instance (default now) and the current instance. |
|
375 | + * |
|
376 | + * @param \Carbon\Carbon|\DateTimeInterface|mixed $date |
|
377 | + * |
|
378 | + * @return static |
|
379 | + */ |
|
380 | + public function min($date = null) |
|
381 | + { |
|
382 | + $date = $this->resolveCarbon($date); |
|
383 | + |
|
384 | + return $this->lt($date) ? $this : $date; |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Get the minimum instance between a given instance (default now) and the current instance. |
|
389 | + * |
|
390 | + * @param \Carbon\Carbon|\DateTimeInterface|mixed $date |
|
391 | + * |
|
392 | + * @see min() |
|
393 | + * |
|
394 | + * @return static |
|
395 | + */ |
|
396 | + public function minimum($date = null) |
|
397 | + { |
|
398 | + return $this->min($date); |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * Get the maximum instance between a given instance (default now) and the current instance. |
|
403 | + * |
|
404 | + * @param \Carbon\Carbon|\DateTimeInterface|mixed $date |
|
405 | + * |
|
406 | + * @return static |
|
407 | + */ |
|
408 | + public function max($date = null) |
|
409 | + { |
|
410 | + $date = $this->resolveCarbon($date); |
|
411 | + |
|
412 | + return $this->gt($date) ? $this : $date; |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Get the maximum instance between a given instance (default now) and the current instance. |
|
417 | + * |
|
418 | + * @param \Carbon\Carbon|\DateTimeInterface|mixed $date |
|
419 | + * |
|
420 | + * @see max() |
|
421 | + * |
|
422 | + * @return static |
|
423 | + */ |
|
424 | + public function maximum($date = null) |
|
425 | + { |
|
426 | + return $this->max($date); |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Calls \DateTime::modify if mutable or \DateTimeImmutable::modify else. |
|
431 | + * |
|
432 | + * @see https://php.net/manual/en/datetime.modify.php |
|
433 | + * |
|
434 | + * @return static|false |
|
435 | + */ |
|
436 | + #[ReturnTypeWillChange] |
|
437 | + public function modify($modify) |
|
438 | + { |
|
439 | + return parent::modify((string) $modify); |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * Similar to native modify() method of DateTime but can handle more grammars. |
|
444 | + * |
|
445 | + * @example |
|
446 | + * ``` |
|
447 | + * echo Carbon::now()->change('next 2pm'); |
|
448 | + * ``` |
|
449 | + * |
|
450 | + * @link https://php.net/manual/en/datetime.modify.php |
|
451 | + * |
|
452 | + * @param string $modifier |
|
453 | + * |
|
454 | + * @return static |
|
455 | + */ |
|
456 | + public function change($modifier) |
|
457 | + { |
|
458 | + return $this->modify(preg_replace_callback('/^(next|previous|last)\s+(\d{1,2}(h|am|pm|:\d{1,2}(:\d{1,2})?))$/i', function ($match) { |
|
459 | + $match[2] = str_replace('h', ':00', $match[2]); |
|
460 | + $test = $this->avoidMutation()->modify($match[2]); |
|
461 | + $method = $match[1] === 'next' ? 'lt' : 'gt'; |
|
462 | + $match[1] = $test->$method($this) ? $match[1].' day' : 'today'; |
|
463 | + |
|
464 | + return $match[1].' '.$match[2]; |
|
465 | + }, strtr(trim($modifier), [ |
|
466 | + ' at ' => ' ', |
|
467 | + 'just now' => 'now', |
|
468 | + 'after tomorrow' => 'tomorrow +1 day', |
|
469 | + 'before yesterday' => 'yesterday -1 day', |
|
470 | + ]))); |
|
471 | + } |
|
472 | 472 | } |
@@ -455,7 +455,7 @@ |
||
455 | 455 | */ |
456 | 456 | public function change($modifier) |
457 | 457 | { |
458 | - return $this->modify(preg_replace_callback('/^(next|previous|last)\s+(\d{1,2}(h|am|pm|:\d{1,2}(:\d{1,2})?))$/i', function ($match) { |
|
458 | + return $this->modify(preg_replace_callback('/^(next|previous|last)\s+(\d{1,2}(h|am|pm|:\d{1,2}(:\d{1,2})?))$/i', function($match) { |
|
459 | 459 | $match[2] = str_replace('h', ':00', $match[2]); |
460 | 460 | $test = $this->avoidMutation()->modify($match[2]); |
461 | 461 | $method = $match[1] === 'next' ? 'lt' : 'gt'; |
@@ -25,167 +25,167 @@ |
||
25 | 25 | */ |
26 | 26 | trait Mixin |
27 | 27 | { |
28 | - /** |
|
29 | - * Stack of macro instance contexts. |
|
30 | - * |
|
31 | - * @var array |
|
32 | - */ |
|
33 | - protected static $macroContextStack = []; |
|
34 | - |
|
35 | - /** |
|
36 | - * Mix another object into the class. |
|
37 | - * |
|
38 | - * @example |
|
39 | - * ``` |
|
40 | - * Carbon::mixin(new class { |
|
41 | - * public function addMoon() { |
|
42 | - * return function () { |
|
43 | - * return $this->addDays(30); |
|
44 | - * }; |
|
45 | - * } |
|
46 | - * public function subMoon() { |
|
47 | - * return function () { |
|
48 | - * return $this->subDays(30); |
|
49 | - * }; |
|
50 | - * } |
|
51 | - * }); |
|
52 | - * $fullMoon = Carbon::create('2018-12-22'); |
|
53 | - * $nextFullMoon = $fullMoon->addMoon(); |
|
54 | - * $blackMoon = Carbon::create('2019-01-06'); |
|
55 | - * $previousBlackMoon = $blackMoon->subMoon(); |
|
56 | - * echo "$nextFullMoon\n"; |
|
57 | - * echo "$previousBlackMoon\n"; |
|
58 | - * ``` |
|
59 | - * |
|
60 | - * @param object|string $mixin |
|
61 | - * |
|
62 | - * @throws ReflectionException |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public static function mixin($mixin) |
|
67 | - { |
|
68 | - \is_string($mixin) && trait_exists($mixin) |
|
69 | - ? self::loadMixinTrait($mixin) |
|
70 | - : self::loadMixinClass($mixin); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @param object|string $mixin |
|
75 | - * |
|
76 | - * @throws ReflectionException |
|
77 | - */ |
|
78 | - private static function loadMixinClass($mixin) |
|
79 | - { |
|
80 | - $methods = (new ReflectionClass($mixin))->getMethods( |
|
81 | - ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED |
|
82 | - ); |
|
83 | - |
|
84 | - foreach ($methods as $method) { |
|
85 | - if ($method->isConstructor() || $method->isDestructor()) { |
|
86 | - continue; |
|
87 | - } |
|
88 | - |
|
89 | - $method->setAccessible(true); |
|
90 | - |
|
91 | - static::macro($method->name, $method->invoke($mixin)); |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @param string $trait |
|
97 | - */ |
|
98 | - private static function loadMixinTrait($trait) |
|
99 | - { |
|
100 | - $context = eval(self::getAnonymousClassCodeForTrait($trait)); |
|
101 | - $className = \get_class($context); |
|
102 | - |
|
103 | - foreach (self::getMixableMethods($context) as $name) { |
|
104 | - $closureBase = Closure::fromCallable([$context, $name]); |
|
105 | - |
|
106 | - static::macro($name, function () use ($closureBase, $className) { |
|
107 | - /** @phpstan-ignore-next-line */ |
|
108 | - $context = isset($this) ? $this->cast($className) : new $className(); |
|
109 | - |
|
110 | - try { |
|
111 | - // @ is required to handle error if not converted into exceptions |
|
112 | - $closure = @$closureBase->bindTo($context); |
|
113 | - } catch (Throwable $throwable) { // @codeCoverageIgnore |
|
114 | - $closure = $closureBase; // @codeCoverageIgnore |
|
115 | - } |
|
116 | - |
|
117 | - // in case of errors not converted into exceptions |
|
118 | - $closure = $closure ?: $closureBase; |
|
119 | - |
|
120 | - return $closure(...\func_get_args()); |
|
121 | - }); |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - private static function getAnonymousClassCodeForTrait(string $trait) |
|
126 | - { |
|
127 | - return 'return new class() extends '.static::class.' {use '.$trait.';};'; |
|
128 | - } |
|
129 | - |
|
130 | - private static function getMixableMethods(self $context): Generator |
|
131 | - { |
|
132 | - foreach (get_class_methods($context) as $name) { |
|
133 | - if (method_exists(static::class, $name)) { |
|
134 | - continue; |
|
135 | - } |
|
136 | - |
|
137 | - yield $name; |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Stack a Carbon context from inside calls of self::this() and execute a given action. |
|
143 | - * |
|
144 | - * @param static|null $context |
|
145 | - * @param callable $callable |
|
146 | - * |
|
147 | - * @throws Throwable |
|
148 | - * |
|
149 | - * @return mixed |
|
150 | - */ |
|
151 | - protected static function bindMacroContext($context, callable $callable) |
|
152 | - { |
|
153 | - static::$macroContextStack[] = $context; |
|
154 | - $exception = null; |
|
155 | - $result = null; |
|
156 | - |
|
157 | - try { |
|
158 | - $result = $callable(); |
|
159 | - } catch (Throwable $throwable) { |
|
160 | - $exception = $throwable; |
|
161 | - } |
|
162 | - |
|
163 | - array_pop(static::$macroContextStack); |
|
164 | - |
|
165 | - if ($exception) { |
|
166 | - throw $exception; |
|
167 | - } |
|
168 | - |
|
169 | - return $result; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Return the current context from inside a macro callee or a null if static. |
|
174 | - * |
|
175 | - * @return static|null |
|
176 | - */ |
|
177 | - protected static function context() |
|
178 | - { |
|
179 | - return end(static::$macroContextStack) ?: null; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Return the current context from inside a macro callee or a new one if static. |
|
184 | - * |
|
185 | - * @return static |
|
186 | - */ |
|
187 | - protected static function this() |
|
188 | - { |
|
189 | - return end(static::$macroContextStack) ?: new static(); |
|
190 | - } |
|
28 | + /** |
|
29 | + * Stack of macro instance contexts. |
|
30 | + * |
|
31 | + * @var array |
|
32 | + */ |
|
33 | + protected static $macroContextStack = []; |
|
34 | + |
|
35 | + /** |
|
36 | + * Mix another object into the class. |
|
37 | + * |
|
38 | + * @example |
|
39 | + * ``` |
|
40 | + * Carbon::mixin(new class { |
|
41 | + * public function addMoon() { |
|
42 | + * return function () { |
|
43 | + * return $this->addDays(30); |
|
44 | + * }; |
|
45 | + * } |
|
46 | + * public function subMoon() { |
|
47 | + * return function () { |
|
48 | + * return $this->subDays(30); |
|
49 | + * }; |
|
50 | + * } |
|
51 | + * }); |
|
52 | + * $fullMoon = Carbon::create('2018-12-22'); |
|
53 | + * $nextFullMoon = $fullMoon->addMoon(); |
|
54 | + * $blackMoon = Carbon::create('2019-01-06'); |
|
55 | + * $previousBlackMoon = $blackMoon->subMoon(); |
|
56 | + * echo "$nextFullMoon\n"; |
|
57 | + * echo "$previousBlackMoon\n"; |
|
58 | + * ``` |
|
59 | + * |
|
60 | + * @param object|string $mixin |
|
61 | + * |
|
62 | + * @throws ReflectionException |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public static function mixin($mixin) |
|
67 | + { |
|
68 | + \is_string($mixin) && trait_exists($mixin) |
|
69 | + ? self::loadMixinTrait($mixin) |
|
70 | + : self::loadMixinClass($mixin); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @param object|string $mixin |
|
75 | + * |
|
76 | + * @throws ReflectionException |
|
77 | + */ |
|
78 | + private static function loadMixinClass($mixin) |
|
79 | + { |
|
80 | + $methods = (new ReflectionClass($mixin))->getMethods( |
|
81 | + ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED |
|
82 | + ); |
|
83 | + |
|
84 | + foreach ($methods as $method) { |
|
85 | + if ($method->isConstructor() || $method->isDestructor()) { |
|
86 | + continue; |
|
87 | + } |
|
88 | + |
|
89 | + $method->setAccessible(true); |
|
90 | + |
|
91 | + static::macro($method->name, $method->invoke($mixin)); |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @param string $trait |
|
97 | + */ |
|
98 | + private static function loadMixinTrait($trait) |
|
99 | + { |
|
100 | + $context = eval(self::getAnonymousClassCodeForTrait($trait)); |
|
101 | + $className = \get_class($context); |
|
102 | + |
|
103 | + foreach (self::getMixableMethods($context) as $name) { |
|
104 | + $closureBase = Closure::fromCallable([$context, $name]); |
|
105 | + |
|
106 | + static::macro($name, function () use ($closureBase, $className) { |
|
107 | + /** @phpstan-ignore-next-line */ |
|
108 | + $context = isset($this) ? $this->cast($className) : new $className(); |
|
109 | + |
|
110 | + try { |
|
111 | + // @ is required to handle error if not converted into exceptions |
|
112 | + $closure = @$closureBase->bindTo($context); |
|
113 | + } catch (Throwable $throwable) { // @codeCoverageIgnore |
|
114 | + $closure = $closureBase; // @codeCoverageIgnore |
|
115 | + } |
|
116 | + |
|
117 | + // in case of errors not converted into exceptions |
|
118 | + $closure = $closure ?: $closureBase; |
|
119 | + |
|
120 | + return $closure(...\func_get_args()); |
|
121 | + }); |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + private static function getAnonymousClassCodeForTrait(string $trait) |
|
126 | + { |
|
127 | + return 'return new class() extends '.static::class.' {use '.$trait.';};'; |
|
128 | + } |
|
129 | + |
|
130 | + private static function getMixableMethods(self $context): Generator |
|
131 | + { |
|
132 | + foreach (get_class_methods($context) as $name) { |
|
133 | + if (method_exists(static::class, $name)) { |
|
134 | + continue; |
|
135 | + } |
|
136 | + |
|
137 | + yield $name; |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Stack a Carbon context from inside calls of self::this() and execute a given action. |
|
143 | + * |
|
144 | + * @param static|null $context |
|
145 | + * @param callable $callable |
|
146 | + * |
|
147 | + * @throws Throwable |
|
148 | + * |
|
149 | + * @return mixed |
|
150 | + */ |
|
151 | + protected static function bindMacroContext($context, callable $callable) |
|
152 | + { |
|
153 | + static::$macroContextStack[] = $context; |
|
154 | + $exception = null; |
|
155 | + $result = null; |
|
156 | + |
|
157 | + try { |
|
158 | + $result = $callable(); |
|
159 | + } catch (Throwable $throwable) { |
|
160 | + $exception = $throwable; |
|
161 | + } |
|
162 | + |
|
163 | + array_pop(static::$macroContextStack); |
|
164 | + |
|
165 | + if ($exception) { |
|
166 | + throw $exception; |
|
167 | + } |
|
168 | + |
|
169 | + return $result; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Return the current context from inside a macro callee or a null if static. |
|
174 | + * |
|
175 | + * @return static|null |
|
176 | + */ |
|
177 | + protected static function context() |
|
178 | + { |
|
179 | + return end(static::$macroContextStack) ?: null; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Return the current context from inside a macro callee or a new one if static. |
|
184 | + * |
|
185 | + * @return static |
|
186 | + */ |
|
187 | + protected static function this() |
|
188 | + { |
|
189 | + return end(static::$macroContextStack) ?: new static(); |
|
190 | + } |
|
191 | 191 | } |
@@ -103,7 +103,7 @@ |
||
103 | 103 | foreach (self::getMixableMethods($context) as $name) { |
104 | 104 | $closureBase = Closure::fromCallable([$context, $name]); |
105 | 105 | |
106 | - static::macro($name, function () use ($closureBase, $className) { |
|
106 | + static::macro($name, function() use ($closureBase, $className) { |
|
107 | 107 | /** @phpstan-ignore-next-line */ |
108 | 108 | $context = isset($this) ? $this->cast($className) : new $className(); |
109 | 109 |
@@ -16,183 +16,183 @@ |
||
16 | 16 | */ |
17 | 17 | trait Timestamp |
18 | 18 | { |
19 | - /** |
|
20 | - * Create a Carbon instance from a timestamp and set the timezone (use default one if not specified). |
|
21 | - * |
|
22 | - * Timestamp input can be given as int, float or a string containing one or more numbers. |
|
23 | - * |
|
24 | - * @param float|int|string $timestamp |
|
25 | - * @param \DateTimeZone|string|null $tz |
|
26 | - * |
|
27 | - * @return static |
|
28 | - */ |
|
29 | - public static function createFromTimestamp($timestamp, $tz = null) |
|
30 | - { |
|
31 | - return static::createFromTimestampUTC($timestamp)->setTimezone($tz); |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * Create a Carbon instance from an timestamp keeping the timezone to UTC. |
|
36 | - * |
|
37 | - * Timestamp input can be given as int, float or a string containing one or more numbers. |
|
38 | - * |
|
39 | - * @param float|int|string $timestamp |
|
40 | - * |
|
41 | - * @return static |
|
42 | - */ |
|
43 | - public static function createFromTimestampUTC($timestamp) |
|
44 | - { |
|
45 | - [$integer, $decimal] = self::getIntegerAndDecimalParts($timestamp); |
|
46 | - $delta = floor($decimal / static::MICROSECONDS_PER_SECOND); |
|
47 | - $integer += $delta; |
|
48 | - $decimal -= $delta * static::MICROSECONDS_PER_SECOND; |
|
49 | - $decimal = str_pad((string) $decimal, 6, '0', STR_PAD_LEFT); |
|
50 | - |
|
51 | - return static::rawCreateFromFormat('U u', "$integer $decimal"); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Create a Carbon instance from a timestamp in milliseconds. |
|
56 | - * |
|
57 | - * Timestamp input can be given as int, float or a string containing one or more numbers. |
|
58 | - * |
|
59 | - * @param float|int|string $timestamp |
|
60 | - * |
|
61 | - * @return static |
|
62 | - */ |
|
63 | - public static function createFromTimestampMsUTC($timestamp) |
|
64 | - { |
|
65 | - [$milliseconds, $microseconds] = self::getIntegerAndDecimalParts($timestamp, 3); |
|
66 | - $sign = $milliseconds < 0 || ($milliseconds === 0.0 && $microseconds < 0) ? -1 : 1; |
|
67 | - $milliseconds = abs($milliseconds); |
|
68 | - $microseconds = $sign * abs($microseconds) + static::MICROSECONDS_PER_MILLISECOND * ($milliseconds % static::MILLISECONDS_PER_SECOND); |
|
69 | - $seconds = $sign * floor($milliseconds / static::MILLISECONDS_PER_SECOND); |
|
70 | - $delta = floor($microseconds / static::MICROSECONDS_PER_SECOND); |
|
71 | - $seconds += $delta; |
|
72 | - $microseconds -= $delta * static::MICROSECONDS_PER_SECOND; |
|
73 | - $microseconds = str_pad($microseconds, 6, '0', STR_PAD_LEFT); |
|
74 | - |
|
75 | - return static::rawCreateFromFormat('U u', "$seconds $microseconds"); |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Create a Carbon instance from a timestamp in milliseconds. |
|
80 | - * |
|
81 | - * Timestamp input can be given as int, float or a string containing one or more numbers. |
|
82 | - * |
|
83 | - * @param float|int|string $timestamp |
|
84 | - * @param \DateTimeZone|string|null $tz |
|
85 | - * |
|
86 | - * @return static |
|
87 | - */ |
|
88 | - public static function createFromTimestampMs($timestamp, $tz = null) |
|
89 | - { |
|
90 | - return static::createFromTimestampMsUTC($timestamp) |
|
91 | - ->setTimezone($tz); |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Set the instance's timestamp. |
|
96 | - * |
|
97 | - * Timestamp input can be given as int, float or a string containing one or more numbers. |
|
98 | - * |
|
99 | - * @param float|int|string $unixTimestamp |
|
100 | - * |
|
101 | - * @return static |
|
102 | - */ |
|
103 | - public function timestamp($unixTimestamp) |
|
104 | - { |
|
105 | - return $this->setTimestamp($unixTimestamp); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Returns a timestamp rounded with the given precision (6 by default). |
|
110 | - * |
|
111 | - * @example getPreciseTimestamp() 1532087464437474 (microsecond maximum precision) |
|
112 | - * @example getPreciseTimestamp(6) 1532087464437474 |
|
113 | - * @example getPreciseTimestamp(5) 153208746443747 (1/100000 second precision) |
|
114 | - * @example getPreciseTimestamp(4) 15320874644375 (1/10000 second precision) |
|
115 | - * @example getPreciseTimestamp(3) 1532087464437 (millisecond precision) |
|
116 | - * @example getPreciseTimestamp(2) 153208746444 (1/100 second precision) |
|
117 | - * @example getPreciseTimestamp(1) 15320874644 (1/10 second precision) |
|
118 | - * @example getPreciseTimestamp(0) 1532087464 (second precision) |
|
119 | - * @example getPreciseTimestamp(-1) 153208746 (10 second precision) |
|
120 | - * @example getPreciseTimestamp(-2) 15320875 (100 second precision) |
|
121 | - * |
|
122 | - * @param int $precision |
|
123 | - * |
|
124 | - * @return float |
|
125 | - */ |
|
126 | - public function getPreciseTimestamp($precision = 6) |
|
127 | - { |
|
128 | - return round(((float) $this->rawFormat('Uu')) / pow(10, 6 - $precision)); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Returns the milliseconds timestamps used amongst other by Date javascript objects. |
|
133 | - * |
|
134 | - * @return float |
|
135 | - */ |
|
136 | - public function valueOf() |
|
137 | - { |
|
138 | - return $this->getPreciseTimestamp(3); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Returns the timestamp with millisecond precision. |
|
143 | - * |
|
144 | - * @return int |
|
145 | - */ |
|
146 | - public function getTimestampMs() |
|
147 | - { |
|
148 | - return (int) $this->getPreciseTimestamp(3); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * @alias getTimestamp |
|
153 | - * |
|
154 | - * Returns the UNIX timestamp for the current date. |
|
155 | - * |
|
156 | - * @return int |
|
157 | - */ |
|
158 | - public function unix() |
|
159 | - { |
|
160 | - return $this->getTimestamp(); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Return an array with integer part digits and decimals digits split from one or more positive numbers |
|
165 | - * (such as timestamps) as string with the given number of decimals (6 by default). |
|
166 | - * |
|
167 | - * By splitting integer and decimal, this method obtain a better precision than |
|
168 | - * number_format when the input is a string. |
|
169 | - * |
|
170 | - * @param float|int|string $numbers one or more numbers |
|
171 | - * @param int $decimals number of decimals precision (6 by default) |
|
172 | - * |
|
173 | - * @return array 0-index is integer part, 1-index is decimal part digits |
|
174 | - */ |
|
175 | - private static function getIntegerAndDecimalParts($numbers, $decimals = 6) |
|
176 | - { |
|
177 | - if (\is_int($numbers) || \is_float($numbers)) { |
|
178 | - $numbers = number_format($numbers, $decimals, '.', ''); |
|
179 | - } |
|
180 | - |
|
181 | - $sign = str_starts_with($numbers, '-') ? -1 : 1; |
|
182 | - $integer = 0; |
|
183 | - $decimal = 0; |
|
184 | - |
|
185 | - foreach (preg_split('`[^\d.]+`', $numbers) as $chunk) { |
|
186 | - [$integerPart, $decimalPart] = explode('.', "$chunk."); |
|
187 | - |
|
188 | - $integer += (int) $integerPart; |
|
189 | - $decimal += (float) ("0.$decimalPart"); |
|
190 | - } |
|
191 | - |
|
192 | - $overflow = floor($decimal); |
|
193 | - $integer += $overflow; |
|
194 | - $decimal -= $overflow; |
|
195 | - |
|
196 | - return [$sign * $integer, $decimal === 0.0 ? 0.0 : $sign * round($decimal * pow(10, $decimals))]; |
|
197 | - } |
|
19 | + /** |
|
20 | + * Create a Carbon instance from a timestamp and set the timezone (use default one if not specified). |
|
21 | + * |
|
22 | + * Timestamp input can be given as int, float or a string containing one or more numbers. |
|
23 | + * |
|
24 | + * @param float|int|string $timestamp |
|
25 | + * @param \DateTimeZone|string|null $tz |
|
26 | + * |
|
27 | + * @return static |
|
28 | + */ |
|
29 | + public static function createFromTimestamp($timestamp, $tz = null) |
|
30 | + { |
|
31 | + return static::createFromTimestampUTC($timestamp)->setTimezone($tz); |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * Create a Carbon instance from an timestamp keeping the timezone to UTC. |
|
36 | + * |
|
37 | + * Timestamp input can be given as int, float or a string containing one or more numbers. |
|
38 | + * |
|
39 | + * @param float|int|string $timestamp |
|
40 | + * |
|
41 | + * @return static |
|
42 | + */ |
|
43 | + public static function createFromTimestampUTC($timestamp) |
|
44 | + { |
|
45 | + [$integer, $decimal] = self::getIntegerAndDecimalParts($timestamp); |
|
46 | + $delta = floor($decimal / static::MICROSECONDS_PER_SECOND); |
|
47 | + $integer += $delta; |
|
48 | + $decimal -= $delta * static::MICROSECONDS_PER_SECOND; |
|
49 | + $decimal = str_pad((string) $decimal, 6, '0', STR_PAD_LEFT); |
|
50 | + |
|
51 | + return static::rawCreateFromFormat('U u', "$integer $decimal"); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Create a Carbon instance from a timestamp in milliseconds. |
|
56 | + * |
|
57 | + * Timestamp input can be given as int, float or a string containing one or more numbers. |
|
58 | + * |
|
59 | + * @param float|int|string $timestamp |
|
60 | + * |
|
61 | + * @return static |
|
62 | + */ |
|
63 | + public static function createFromTimestampMsUTC($timestamp) |
|
64 | + { |
|
65 | + [$milliseconds, $microseconds] = self::getIntegerAndDecimalParts($timestamp, 3); |
|
66 | + $sign = $milliseconds < 0 || ($milliseconds === 0.0 && $microseconds < 0) ? -1 : 1; |
|
67 | + $milliseconds = abs($milliseconds); |
|
68 | + $microseconds = $sign * abs($microseconds) + static::MICROSECONDS_PER_MILLISECOND * ($milliseconds % static::MILLISECONDS_PER_SECOND); |
|
69 | + $seconds = $sign * floor($milliseconds / static::MILLISECONDS_PER_SECOND); |
|
70 | + $delta = floor($microseconds / static::MICROSECONDS_PER_SECOND); |
|
71 | + $seconds += $delta; |
|
72 | + $microseconds -= $delta * static::MICROSECONDS_PER_SECOND; |
|
73 | + $microseconds = str_pad($microseconds, 6, '0', STR_PAD_LEFT); |
|
74 | + |
|
75 | + return static::rawCreateFromFormat('U u', "$seconds $microseconds"); |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Create a Carbon instance from a timestamp in milliseconds. |
|
80 | + * |
|
81 | + * Timestamp input can be given as int, float or a string containing one or more numbers. |
|
82 | + * |
|
83 | + * @param float|int|string $timestamp |
|
84 | + * @param \DateTimeZone|string|null $tz |
|
85 | + * |
|
86 | + * @return static |
|
87 | + */ |
|
88 | + public static function createFromTimestampMs($timestamp, $tz = null) |
|
89 | + { |
|
90 | + return static::createFromTimestampMsUTC($timestamp) |
|
91 | + ->setTimezone($tz); |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Set the instance's timestamp. |
|
96 | + * |
|
97 | + * Timestamp input can be given as int, float or a string containing one or more numbers. |
|
98 | + * |
|
99 | + * @param float|int|string $unixTimestamp |
|
100 | + * |
|
101 | + * @return static |
|
102 | + */ |
|
103 | + public function timestamp($unixTimestamp) |
|
104 | + { |
|
105 | + return $this->setTimestamp($unixTimestamp); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Returns a timestamp rounded with the given precision (6 by default). |
|
110 | + * |
|
111 | + * @example getPreciseTimestamp() 1532087464437474 (microsecond maximum precision) |
|
112 | + * @example getPreciseTimestamp(6) 1532087464437474 |
|
113 | + * @example getPreciseTimestamp(5) 153208746443747 (1/100000 second precision) |
|
114 | + * @example getPreciseTimestamp(4) 15320874644375 (1/10000 second precision) |
|
115 | + * @example getPreciseTimestamp(3) 1532087464437 (millisecond precision) |
|
116 | + * @example getPreciseTimestamp(2) 153208746444 (1/100 second precision) |
|
117 | + * @example getPreciseTimestamp(1) 15320874644 (1/10 second precision) |
|
118 | + * @example getPreciseTimestamp(0) 1532087464 (second precision) |
|
119 | + * @example getPreciseTimestamp(-1) 153208746 (10 second precision) |
|
120 | + * @example getPreciseTimestamp(-2) 15320875 (100 second precision) |
|
121 | + * |
|
122 | + * @param int $precision |
|
123 | + * |
|
124 | + * @return float |
|
125 | + */ |
|
126 | + public function getPreciseTimestamp($precision = 6) |
|
127 | + { |
|
128 | + return round(((float) $this->rawFormat('Uu')) / pow(10, 6 - $precision)); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Returns the milliseconds timestamps used amongst other by Date javascript objects. |
|
133 | + * |
|
134 | + * @return float |
|
135 | + */ |
|
136 | + public function valueOf() |
|
137 | + { |
|
138 | + return $this->getPreciseTimestamp(3); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Returns the timestamp with millisecond precision. |
|
143 | + * |
|
144 | + * @return int |
|
145 | + */ |
|
146 | + public function getTimestampMs() |
|
147 | + { |
|
148 | + return (int) $this->getPreciseTimestamp(3); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * @alias getTimestamp |
|
153 | + * |
|
154 | + * Returns the UNIX timestamp for the current date. |
|
155 | + * |
|
156 | + * @return int |
|
157 | + */ |
|
158 | + public function unix() |
|
159 | + { |
|
160 | + return $this->getTimestamp(); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Return an array with integer part digits and decimals digits split from one or more positive numbers |
|
165 | + * (such as timestamps) as string with the given number of decimals (6 by default). |
|
166 | + * |
|
167 | + * By splitting integer and decimal, this method obtain a better precision than |
|
168 | + * number_format when the input is a string. |
|
169 | + * |
|
170 | + * @param float|int|string $numbers one or more numbers |
|
171 | + * @param int $decimals number of decimals precision (6 by default) |
|
172 | + * |
|
173 | + * @return array 0-index is integer part, 1-index is decimal part digits |
|
174 | + */ |
|
175 | + private static function getIntegerAndDecimalParts($numbers, $decimals = 6) |
|
176 | + { |
|
177 | + if (\is_int($numbers) || \is_float($numbers)) { |
|
178 | + $numbers = number_format($numbers, $decimals, '.', ''); |
|
179 | + } |
|
180 | + |
|
181 | + $sign = str_starts_with($numbers, '-') ? -1 : 1; |
|
182 | + $integer = 0; |
|
183 | + $decimal = 0; |
|
184 | + |
|
185 | + foreach (preg_split('`[^\d.]+`', $numbers) as $chunk) { |
|
186 | + [$integerPart, $decimalPart] = explode('.', "$chunk."); |
|
187 | + |
|
188 | + $integer += (int) $integerPart; |
|
189 | + $decimal += (float) ("0.$decimalPart"); |
|
190 | + } |
|
191 | + |
|
192 | + $overflow = floor($decimal); |
|
193 | + $integer += $overflow; |
|
194 | + $decimal -= $overflow; |
|
195 | + |
|
196 | + return [$sign * $integer, $decimal === 0.0 ? 0.0 : $sign * round($decimal * pow(10, $decimals))]; |
|
197 | + } |
|
198 | 198 | } |