Debian Codegraph @

lib/3rdParty/composer/nesbot/carbon/src/Carbon/Traits/Difference.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 /**
6 * This file is part of the Carbon package.
7 *
8 * (c) Brian Nesbitt <[email protected]>
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13
14 namespace Carbon\Traits;
15
16 use Carbon\Carbon;
17 use Carbon\CarbonImmutable;
18 use Carbon\CarbonInterface;
19 use Carbon\CarbonInterval;
20 use Carbon\CarbonPeriod;
21 use Carbon\Exceptions\UnknownUnitException;
22 use Carbon\Unit;
23 use Closure;
24 use DateInterval;
25 use DateTimeInterface;
26
27 /**
28 * Trait Difference.
29 *
30 * Depends on the following methods:
31 *
32 * @method bool lessThan($date)
33 * @method static copy()
34 * @method static resolveCarbon($date = null)
35 */
36 trait Difference
37 {
38 /**
39 * Get the difference as a DateInterval instance.
40 * Return relative interval (negative if $absolute flag is not set to true and the given date is before
41 * current one).
42 *
43 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
44 * @param bool $absolute Get the absolute of the difference
45 *
46 * @return DateInterval
47 */
48 public function diffAsDateInterval($date = null, bool $absolute = false): DateInterval
49 {
50 $other = $this->resolveCarbon($date);
51
52 // Work-around for https://bugs.php.net/bug.php?id=81458
53 // It was initially introduced for https://bugs.php.net/bug.php?id=80998
54 // The very specific case of 80998 was fixed in PHP 8.1beta3, but it introduced 81458
55 // So we still need to keep this for now
56 if ($other->tz !== $this->tz) {
57 $other = $other->avoidMutation()->setTimezone($this->tz);
58 }
59
60 return parent::diff($other, $absolute);
61 }
62
63 /**
64 * Get the difference as a CarbonInterval instance.
65 * Return relative interval (negative if $absolute flag is not set to true and the given date is before
66 * current one).
67 *
68 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
69 * @param bool $absolute Get the absolute of the difference
70 *
71 * @return CarbonInterval
72 */
73 public function diffAsCarbonInterval($date = null, bool $absolute = false, array $skip = []): CarbonInterval
74 {
75 return CarbonInterval::diff($this, $this->resolveCarbon($date), $absolute, $skip)
76 ->setLocalTranslator($this->getLocalTranslator());
77 }
78
79 /**
80 * @alias diffAsCarbonInterval
81 *
82 * Get the difference as a DateInterval instance.
83 * Return relative interval (negative if $absolute flag is not set to true and the given date is before
84 * current one).
85 *
86 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
87 * @param bool $absolute Get the absolute of the difference
88 *
89 * @return CarbonInterval
90 */
91 public function diff($date = null, bool $absolute = false, array $skip = []): CarbonInterval
92 {
93 return $this->diffAsCarbonInterval($date, $absolute, $skip);
94 }
95
96 /**
97 * @param Unit|string $unit microsecond, millisecond, second, minute,
98 * hour, day, week, month, quarter, year,
99 * century, millennium
100 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
101 * @param bool $absolute Get the absolute of the difference
102 * @param bool $utc Always convert dates to UTC before comparing (if not set, it will do it only if timezones are different)
103 *
104 * @return float
105 */
106 public function diffInUnit(Unit|string $unit, $date = null, bool $absolute = false, bool $utc = false): float
107 {
108 $unit = static::pluralUnit($unit instanceof Unit ? $unit->value : rtrim($unit, 'z'));
109 $method = 'diffIn'.$unit;
110
111 if (!method_exists($this, $method)) {
112 throw new UnknownUnitException($unit);
113 }
114
115 return $this->$method($date, $absolute, $utc);
116 }
117
118 /**
119 * Get the difference in years
120 *
121 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
122 * @param bool $absolute Get the absolute of the difference
123 * @param bool $utc Always convert dates to UTC before comparing (if not set, it will do it only if timezones are different)
124 *
125 * @return float
126 */
127 public function diffInYears($date = null, bool $absolute = false, bool $utc = false): float
128 {
129 $start = $this;
130 $end = $this->resolveCarbon($date);
131
132 if ($utc) {
133 $start = $start->avoidMutation()->utc();
134 $end = $end->avoidMutation()->utc();
135 }
136
137 $ascending = ($start <= $end);
138 $sign = $absolute || $ascending ? 1 : -1;
139
140 if (!$ascending) {
141 [$start, $end] = [$end, $start];
142 }
143
144 $yearsDiff = (int) $start->diff($end, $absolute)->format('%r%y');
145 /** @var Carbon|CarbonImmutable $floorEnd */
146 $floorEnd = $start->avoidMutation()->addYears($yearsDiff);
147
148 if ($floorEnd >= $end) {
149 return $sign * $yearsDiff;
150 }
151
152 /** @var Carbon|CarbonImmutable $ceilEnd */
153 $ceilEnd = $start->avoidMutation()->addYears($yearsDiff + 1);
154
155 $daysToFloor = $floorEnd->diffInDays($end);
156 $daysToCeil = $end->diffInDays($ceilEnd);
157
158 return $sign * ($yearsDiff + $daysToFloor / ($daysToCeil + $daysToFloor));
159 }
160
161 /**
162 * Get the difference in quarters.
163 *
164 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
165 * @param bool $absolute Get the absolute of the difference
166 * @param bool $utc Always convert dates to UTC before comparing (if not set, it will do it only if timezones are different)
167 *
168 * @return float
169 */
170 public function diffInQuarters($date = null, bool $absolute = false, bool $utc = false): float
171 {
172 return $this->diffInMonths($date, $absolute, $utc) / static::MONTHS_PER_QUARTER;
173 }
174
175 /**
176 * Get the difference in months.
177 *
178 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
179 * @param bool $absolute Get the absolute of the difference
180 * @param bool $utc Always convert dates to UTC before comparing (if not set, it will do it only if timezones are different)
181 *
182 * @return float
183 */
184 public function diffInMonths($date = null, bool $absolute = false, bool $utc = false): float
185 {
186 $start = $this;
187 $end = $this->resolveCarbon($date);
188
189 // Compare using UTC
190 if ($utc || ($end->timezoneName !== $start->timezoneName)) {
191 $start = $start->avoidMutation()->utc();
192 $end = $end->avoidMutation()->utc();
193 }
194
195 [$yearStart, $monthStart, $dayStart] = explode('-', $start->format('Y-m-dHisu'));
196 [$yearEnd, $monthEnd, $dayEnd] = explode('-', $end->format('Y-m-dHisu'));
197
198 $monthsDiff = (((int) $yearEnd) - ((int) $yearStart)) * static::MONTHS_PER_YEAR +
199 ((int) $monthEnd) - ((int) $monthStart);
200
201 if ($monthsDiff > 0) {
202 $monthsDiff -= ($dayStart > $dayEnd ? 1 : 0);
203 } elseif ($monthsDiff < 0) {
204 $monthsDiff += ($dayStart < $dayEnd ? 1 : 0);
205 }
206
207 $ascending = ($start <= $end);
208 $sign = $absolute || $ascending ? 1 : -1;
209 $monthsDiff = abs($monthsDiff);
210
211 if (!$ascending) {
212 [$start, $end] = [$end, $start];
213 }
214
215 /** @var Carbon|CarbonImmutable $floorEnd */
216 $floorEnd = $start->avoidMutation()->addMonths($monthsDiff);
217
218 if ($floorEnd >= $end) {
219 return $sign * $monthsDiff;
220 }
221
222 /** @var Carbon|CarbonImmutable $ceilEnd */
223 $ceilEnd = $start->avoidMutation()->addMonths($monthsDiff + 1);
224
225 $daysToFloor = $floorEnd->diffInDays($end);
226 $daysToCeil = $end->diffInDays($ceilEnd);
227
228 return $sign * ($monthsDiff + $daysToFloor / ($daysToCeil + $daysToFloor));
229 }
230
231 /**
232 * Get the difference in weeks.
233 *
234 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
235 * @param bool $absolute Get the absolute of the difference
236 * @param bool $utc Always convert dates to UTC before comparing (if not set, it will do it only if timezones are different)
237 *
238 * @return float
239 */
240 public function diffInWeeks($date = null, bool $absolute = false, bool $utc = false): float
241 {
242 return $this->diffInDays($date, $absolute, $utc) / static::DAYS_PER_WEEK;
243 }
244
245 /**
246 * Get the difference in days.
247 *
248 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
249 * @param bool $absolute Get the absolute of the difference
250 * @param bool $utc Always convert dates to UTC before comparing (if not set, it will do it only if timezones are different)
251 *
252 * @return float
253 */
254 public function diffInDays($date = null, bool $absolute = false, bool $utc = false): float
255 {
256 $date = $this->resolveCarbon($date);
257 $current = $this;
258
259 // Compare using UTC
260 if ($utc || ($date->timezoneName !== $current->timezoneName)) {
261 $date = $date->avoidMutation()->utc();
262 $current = $current->avoidMutation()->utc();
263 }
264
265 $negative = ($date < $current);
266 [$start, $end] = $negative ? [$date, $current] : [$current, $date];
267 $interval = $start->diffAsDateInterval($end);
268 $daysA = $this->getIntervalDayDiff($interval);
269 $floorEnd = $start->avoidMutation()->addDays($daysA);
270 $daysB = $daysA + ($floorEnd <= $end ? 1 : -1);
271 $ceilEnd = $start->avoidMutation()->addDays($daysB);
272 $microsecondsBetween = $floorEnd->diffInMicroseconds($ceilEnd);
273 $microsecondsToEnd = $floorEnd->diffInMicroseconds($end);
274
275 return ($negative && !$absolute ? -1 : 1)
276 * ($daysA * ($microsecondsBetween - $microsecondsToEnd) + $daysB * $microsecondsToEnd)
277 / $microsecondsBetween;
278 }
279
280 /**
281 * Get the difference in days using a filter closure.
282 *
283 * @param Closure $callback
284 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
285 * @param bool $absolute Get the absolute of the difference
286 *
287 * @return int
288 */
289 public function diffInDaysFiltered(Closure $callback, $date = null, bool $absolute = false): int
290 {
291 return $this->diffFiltered(CarbonInterval::day(), $callback, $date, $absolute);
292 }
293
294 /**
295 * Get the difference in hours using a filter closure.
296 *
297 * @param Closure $callback
298 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
299 * @param bool $absolute Get the absolute of the difference
300 *
301 * @return int
302 */
303 public function diffInHoursFiltered(Closure $callback, $date = null, bool $absolute = false): int
304 {
305 return $this->diffFiltered(CarbonInterval::hour(), $callback, $date, $absolute);
306 }
307
308 /**
309 * Get the difference by the given interval using a filter closure.
310 *
311 * @param CarbonInterval $ci An interval to traverse by
312 * @param Closure $callback
313 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
314 * @param bool $absolute Get the absolute of the difference
315 *
316 * @return int
317 */
318 public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, bool $absolute = false): int
319 {
320 $start = $this;
321 $end = $this->resolveCarbon($date);
322 $inverse = false;
323
324 if ($end < $start) {
325 $start = $end;
326 $end = $this;
327 $inverse = true;
328 }
329
330 $options = CarbonPeriod::EXCLUDE_END_DATE | ($this->isMutable() ? 0 : CarbonPeriod::IMMUTABLE);
331 $diff = $ci->toPeriod($start, $end, $options)->filter($callback)->count();
332
333 return $inverse && !$absolute ? -$diff : $diff;
334 }
335
336 /**
337 * Get the difference in weekdays.
338 *
339 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
340 * @param bool $absolute Get the absolute of the difference
341 *
342 * @return int
343 */
344 public function diffInWeekdays($date = null, bool $absolute = false): int
345 {
346 return $this->diffInDaysFiltered(
347 static fn (CarbonInterface $date) => $date->isWeekday(),
348 $this->resolveCarbon($date)->avoidMutation()->modify($this->format('H:i:s.u')),
349 $absolute,
350 );
351 }
352
353 /**
354 * Get the difference in weekend days using a filter.
355 *
356 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
357 * @param bool $absolute Get the absolute of the difference
358 *
359 * @return int
360 */
361 public function diffInWeekendDays($date = null, bool $absolute = false): int
362 {
363 return $this->diffInDaysFiltered(
364 static fn (CarbonInterface $date) => $date->isWeekend(),
365 $this->resolveCarbon($date)->avoidMutation()->modify($this->format('H:i:s.u')),
366 $absolute,
367 );
368 }
369
370 /**
371 * Get the difference in hours.
372 *
373 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
374 * @param bool $absolute Get the absolute of the difference
375 *
376 * @return float
377 */
378 public function diffInHours($date = null, bool $absolute = false): float
379 {
380 return $this->diffInMinutes($date, $absolute) / static::MINUTES_PER_HOUR;
381 }
382
383 /**
384 * Get the difference in minutes.
385 *
386 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
387 * @param bool $absolute Get the absolute of the difference
388 *
389 * @return float
390 */
391 public function diffInMinutes($date = null, bool $absolute = false): float
392 {
393 return $this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE;
394 }
395
396 /**
397 * Get the difference in seconds.
398 *
399 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
400 * @param bool $absolute Get the absolute of the difference
401 *
402 * @return float
403 */
404 public function diffInSeconds($date = null, bool $absolute = false): float
405 {
406 return $this->diffInMilliseconds($date, $absolute) / static::MILLISECONDS_PER_SECOND;
407 }
408
409 /**
410 * Get the difference in microseconds.
411 *
412 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
413 * @param bool $absolute Get the absolute of the difference
414 *
415 * @return float
416 */
417 public function diffInMicroseconds($date = null, bool $absolute = false): float
418 {
419 /** @var CarbonInterface $date */
420 $date = $this->resolveCarbon($date);
421 $value = ($date->timestamp - $this->timestamp) * static::MICROSECONDS_PER_SECOND +
422 $date->micro - $this->micro;
423
424 return $absolute ? abs($value) : $value;
425 }
426
427 /**
428 * Get the difference in milliseconds.
429 *
430 * @param \Carbon\CarbonInterface|\DateTimeInterface|string|null $date
431 * @param bool $absolute Get the absolute of the difference
432 *
433 * @return float
434 */
435 public function diffInMilliseconds($date = null, bool $absolute = false): float
436 {
437 return $this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND;
438 }
439
440 /**
441 * The number of seconds since midnight.
442 *
443 * @return float
444 */
445 public function secondsSinceMidnight(): float
446 {
447 return $this->diffInSeconds($this->copy()->startOfDay(), true);
448 }
449
450 /**
451 * The number of seconds until 23:59:59.
452 *
453 * @return float
454 */
455 public function secondsUntilEndOfDay(): float
456 {
457 return $this->diffInSeconds($this->copy()->endOfDay(), true);
458 }
459
460 /**
461 * Get the difference in a human readable format in the current locale from current instance to an other
462 * instance given (or now if null given).
463 *
464 * @example
465 * ```
466 * echo Carbon::tomorrow()->diffForHumans() . "\n";
467 * echo Carbon::tomorrow()->diffForHumans(['parts' => 2]) . "\n";
468 * echo Carbon::tomorrow()->diffForHumans(['parts' => 3, 'join' => true]) . "\n";
469 * echo Carbon::tomorrow()->diffForHumans(Carbon::yesterday()) . "\n";
470 * echo Carbon::tomorrow()->diffForHumans(Carbon::yesterday(), ['short' => true]) . "\n";
471 * ```
472 *
473 * @param Carbon|DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below;
474 * if null passed, now will be used as comparison reference;
475 * if any other type, it will be converted to date and used as reference.
476 * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains:
477 * ⦿ 'syntax' entry (see below)
478 * ⦿ 'short' entry (see below)
479 * ⦿ 'parts' entry (see below)
480 * ⦿ 'options' entry (see below)
481 * ⦿ 'skip' entry, list of units to skip (array of strings or a single string,
482 * ` it can be the unit name (singular or plural) or its shortcut
483 * ` (y, m, w, d, h, min, s, ms, µs).
484 * ⦿ 'aUnit' entry, prefer "an hour" over "1 hour" if true
485 * ⦿ 'altNumbers' entry, use alternative numbers if available
486 * ` (from the current language if true is passed, from the given language(s)
487 * ` if array or string is passed)
488 * ⦿ 'join' entry determines how to join multiple parts of the string
489 * ` - if $join is a string, it's used as a joiner glue
490 * ` - if $join is a callable/closure, it get the list of string and should return a string
491 * ` - if $join is an array, the first item will be the default glue, and the second item
492 * ` will be used instead of the glue for the last item
493 * ` - if $join is true, it will be guessed from the locale ('list' translation file entry)
494 * ` - if $join is missing, a space will be used as glue
495 * ⦿ 'other' entry (see above)
496 * ⦿ 'minimumUnit' entry determines the smallest unit of time to display can be long or
497 * ` short form of the units, e.g. 'hour' or 'h' (default value: s)
498 * ⦿ 'locale' language in which the diff should be output (has no effect if 'translator' key is set)
499 * ⦿ 'translator' a custom translator to use to translator the output.
500 * if int passed, it adds modifiers:
501 * Possible values:
502 * - CarbonInterface::DIFF_ABSOLUTE no modifiers
503 * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier
504 * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier
505 * Default value: CarbonInterface::DIFF_ABSOLUTE
506 * @param bool $short displays short format of time units
507 * @param int $parts maximum number of parts to display (default value: 1: single unit)
508 * @param int $options human diff options
509 */
510 public function diffForHumans($other = null, $syntax = null, $short = false, $parts = 1, $options = null): string
511 {
512 /* @var CarbonInterface $this */
513 if (\is_array($other)) {
514 $other['syntax'] = \array_key_exists('syntax', $other) ? $other['syntax'] : $syntax;
515 $syntax = $other;
516 $other = $syntax['other'] ?? null;
517 }
518
519 $intSyntax = &$syntax;
520
521 if (\is_array($syntax)) {
522 $syntax['syntax'] = $syntax['syntax'] ?? null;
523 $intSyntax = &$syntax['syntax'];
524 }
525
526 $intSyntax = (int) ($intSyntax ?? static::DIFF_RELATIVE_AUTO);
527 $intSyntax = $intSyntax === static::DIFF_RELATIVE_AUTO && $other === null ? static::DIFF_RELATIVE_TO_NOW : $intSyntax;
528
529 $parts = min(7, max(1, (int) $parts));
530 $skip = \is_array($syntax) ? ($syntax['skip'] ?? []) : [];
531 $options ??= $this->localHumanDiffOptions ?? $this->transmitFactory(
532 static fn () => static::getHumanDiffOptions(),
533 );
534
535 return $this->diff($other, skip: (array) $skip)->forHumans($syntax, (bool) $short, $parts, $options);
536 }
537
538 /**
539 * @alias diffForHumans
540 *
541 * Get the difference in a human readable format in the current locale from current instance to an other
542 * instance given (or now if null given).
543 *
544 * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below;
545 * if null passed, now will be used as comparison reference;
546 * if any other type, it will be converted to date and used as reference.
547 * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains:
548 * - 'syntax' entry (see below)
549 * - 'short' entry (see below)
550 * - 'parts' entry (see below)
551 * - 'options' entry (see below)
552 * - 'join' entry determines how to join multiple parts of the string
553 * ` - if $join is a string, it's used as a joiner glue
554 * ` - if $join is a callable/closure, it get the list of string and should return a string
555 * ` - if $join is an array, the first item will be the default glue, and the second item
556 * ` will be used instead of the glue for the last item
557 * ` - if $join is true, it will be guessed from the locale ('list' translation file entry)
558 * ` - if $join is missing, a space will be used as glue
559 * - 'other' entry (see above)
560 * if int passed, it add modifiers:
561 * Possible values:
562 * - CarbonInterface::DIFF_ABSOLUTE no modifiers
563 * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier
564 * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier
565 * Default value: CarbonInterface::DIFF_ABSOLUTE
566 * @param bool $short displays short format of time units
567 * @param int $parts maximum number of parts to display (default value: 1: single unit)
568 * @param int $options human diff options
569 *
570 * @return string
571 */
572 public function from($other = null, $syntax = null, $short = false, $parts = 1, $options = null)
573 {
574 return $this->diffForHumans($other, $syntax, $short, $parts, $options);
575 }
576
577 /**
578 * @alias diffForHumans
579 *
580 * Get the difference in a human readable format in the current locale from current instance to an other
581 * instance given (or now if null given).
582 */
583 public function since($other = null, $syntax = null, $short = false, $parts = 1, $options = null)
584 {
585 return $this->diffForHumans($other, $syntax, $short, $parts, $options);
586 }
587
588 /**
589 * Get the difference in a human readable format in the current locale from an other
590 * instance given (or now if null given) to current instance.
591 *
592 * When comparing a value in the past to default now:
593 * 1 hour from now
594 * 5 months from now
595 *
596 * When comparing a value in the future to default now:
597 * 1 hour ago
598 * 5 months ago
599 *
600 * When comparing a value in the past to another value:
601 * 1 hour after
602 * 5 months after
603 *
604 * When comparing a value in the future to another value:
605 * 1 hour before
606 * 5 months before
607 *
608 * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below;
609 * if null passed, now will be used as comparison reference;
610 * if any other type, it will be converted to date and used as reference.
611 * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains:
612 * - 'syntax' entry (see below)
613 * - 'short' entry (see below)
614 * - 'parts' entry (see below)
615 * - 'options' entry (see below)
616 * - 'join' entry determines how to join multiple parts of the string
617 * ` - if $join is a string, it's used as a joiner glue
618 * ` - if $join is a callable/closure, it get the list of string and should return a string
619 * ` - if $join is an array, the first item will be the default glue, and the second item
620 * ` will be used instead of the glue for the last item
621 * ` - if $join is true, it will be guessed from the locale ('list' translation file entry)
622 * ` - if $join is missing, a space will be used as glue
623 * - 'other' entry (see above)
624 * if int passed, it add modifiers:
625 * Possible values:
626 * - CarbonInterface::DIFF_ABSOLUTE no modifiers
627 * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier
628 * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier
629 * Default value: CarbonInterface::DIFF_ABSOLUTE
630 * @param bool $short displays short format of time units
631 * @param int $parts maximum number of parts to display (default value: 1: single unit)
632 * @param int $options human diff options
633 *
634 * @return string
635 */
636 public function to($other = null, $syntax = null, $short = false, $parts = 1, $options = null)
637 {
638 if (!$syntax && !$other) {
639 $syntax = CarbonInterface::DIFF_RELATIVE_TO_NOW;
640 }
641
642 return $this->resolveCarbon($other)->diffForHumans($this, $syntax, $short, $parts, $options);
643 }
644
645 /**
646 * @alias to
647 *
648 * Get the difference in a human readable format in the current locale from an other
649 * instance given (or now if null given) to current instance.
650 *
651 * @param Carbon|\DateTimeInterface|string|array|null $other if array passed, will be used as parameters array, see $syntax below;
652 * if null passed, now will be used as comparison reference;
653 * if any other type, it will be converted to date and used as reference.
654 * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains:
655 * - 'syntax' entry (see below)
656 * - 'short' entry (see below)
657 * - 'parts' entry (see below)
658 * - 'options' entry (see below)
659 * - 'join' entry determines how to join multiple parts of the string
660 * ` - if $join is a string, it's used as a joiner glue
661 * ` - if $join is a callable/closure, it get the list of string and should return a string
662 * ` - if $join is an array, the first item will be the default glue, and the second item
663 * ` will be used instead of the glue for the last item
664 * ` - if $join is true, it will be guessed from the locale ('list' translation file entry)
665 * ` - if $join is missing, a space will be used as glue
666 * - 'other' entry (see above)
667 * if int passed, it add modifiers:
668 * Possible values:
669 * - CarbonInterface::DIFF_ABSOLUTE no modifiers
670 * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier
671 * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier
672 * Default value: CarbonInterface::DIFF_ABSOLUTE
673 * @param bool $short displays short format of time units
674 * @param int $parts maximum number of parts to display (default value: 1: single unit)
675 * @param int $options human diff options
676 *
677 * @return string
678 */
679 public function until($other = null, $syntax = null, $short = false, $parts = 1, $options = null)
680 {
681 return $this->to($other, $syntax, $short, $parts, $options);
682 }
683
684 /**
685 * Get the difference in a human readable format in the current locale from current
686 * instance to now.
687 *
688 * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains:
689 * - 'syntax' entry (see below)
690 * - 'short' entry (see below)
691 * - 'parts' entry (see below)
692 * - 'options' entry (see below)
693 * - 'join' entry determines how to join multiple parts of the string
694 * ` - if $join is a string, it's used as a joiner glue
695 * ` - if $join is a callable/closure, it get the list of string and should return a string
696 * ` - if $join is an array, the first item will be the default glue, and the second item
697 * ` will be used instead of the glue for the last item
698 * ` - if $join is true, it will be guessed from the locale ('list' translation file entry)
699 * ` - if $join is missing, a space will be used as glue
700 * if int passed, it add modifiers:
701 * Possible values:
702 * - CarbonInterface::DIFF_ABSOLUTE no modifiers
703 * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier
704 * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier
705 * Default value: CarbonInterface::DIFF_ABSOLUTE
706 * @param bool $short displays short format of time units
707 * @param int $parts maximum number of parts to display (default value: 1: single unit)
708 * @param int $options human diff options
709 *
710 * @return string
711 */
712 public function fromNow($syntax = null, $short = false, $parts = 1, $options = null)
713 {
714 $other = null;
715
716 if ($syntax instanceof DateTimeInterface) {
717 [$other, $syntax, $short, $parts, $options] = array_pad(\func_get_args(), 5, null);
718 }
719
720 return $this->from($other, $syntax, $short, $parts, $options);
721 }
722
723 /**
724 * Get the difference in a human readable format in the current locale from an other
725 * instance given to now
726 *
727 * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains:
728 * - 'syntax' entry (see below)
729 * - 'short' entry (see below)
730 * - 'parts' entry (see below)
731 * - 'options' entry (see below)
732 * - 'join' entry determines how to join multiple parts of the string
733 * ` - if $join is a string, it's used as a joiner glue
734 * ` - if $join is a callable/closure, it get the list of string and should return a string
735 * ` - if $join is an array, the first item will be the default glue, and the second item
736 * ` will be used instead of the glue for the last item
737 * ` - if $join is true, it will be guessed from the locale ('list' translation file entry)
738 * ` - if $join is missing, a space will be used as glue
739 * if int passed, it add modifiers:
740 * Possible values:
741 * - CarbonInterface::DIFF_ABSOLUTE no modifiers
742 * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier
743 * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier
744 * Default value: CarbonInterface::DIFF_ABSOLUTE
745 * @param bool $short displays short format of time units
746 * @param int $parts maximum number of parts to display (default value: 1: single part)
747 * @param int $options human diff options
748 *
749 * @return string
750 */
751 public function toNow($syntax = null, $short = false, $parts = 1, $options = null)
752 {
753 return $this->to(null, $syntax, $short, $parts, $options);
754 }
755
756 /**
757 * Get the difference in a human readable format in the current locale from an other
758 * instance given to now
759 *
760 * @param int|array $syntax if array passed, parameters will be extracted from it, the array may contains:
761 * - 'syntax' entry (see below)
762 * - 'short' entry (see below)
763 * - 'parts' entry (see below)
764 * - 'options' entry (see below)
765 * - 'join' entry determines how to join multiple parts of the string
766 * ` - if $join is a string, it's used as a joiner glue
767 * ` - if $join is a callable/closure, it get the list of string and should return a string
768 * ` - if $join is an array, the first item will be the default glue, and the second item
769 * ` will be used instead of the glue for the last item
770 * ` - if $join is true, it will be guessed from the locale ('list' translation file entry)
771 * ` - if $join is missing, a space will be used as glue
772 * if int passed, it add modifiers:
773 * Possible values:
774 * - CarbonInterface::DIFF_ABSOLUTE no modifiers
775 * - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier
776 * - CarbonInterface::DIFF_RELATIVE_TO_OTHER add before/after modifier
777 * Default value: CarbonInterface::DIFF_ABSOLUTE
778 * @param bool $short displays short format of time units
779 * @param int $parts maximum number of parts to display (default value: 1: single part)
780 * @param int $options human diff options
781 *
782 * @return string
783 */
784 public function ago($syntax = null, $short = false, $parts = 1, $options = null)
785 {
786 $other = null;
787
788 if ($syntax instanceof DateTimeInterface) {
789 [$other, $syntax, $short, $parts, $options] = array_pad(\func_get_args(), 5, null);
790 }
791
792 return $this->from($other, $syntax, $short, $parts, $options);
793 }
794
795 /**
796 * Get the difference in a human-readable format in the current locale from current instance to another
797 * instance given (or now if null given).
798 *
799 * @return string
800 */
801 public function timespan($other = null, $timezone = null): string
802 {
803 if (\is_string($other)) {
804 $other = $this->transmitFactory(static fn () => static::parse($other, $timezone));
805 }
806
807 return $this->diffForHumans($other, [
808 'join' => ', ',
809 'syntax' => CarbonInterface::DIFF_ABSOLUTE,
810 'parts' => INF,
811 ]);
812 }
813
814 /**
815 * Returns either day of week + time (e.g. "Last Friday at 3:30 PM") if reference time is within 7 days,
816 * or a calendar date (e.g. "10/29/2017") otherwise.
817 *
818 * Language, date and time formats will change according to the current locale.
819 *
820 * @param Carbon|\DateTimeInterface|string|null $referenceTime
821 * @param array $formats
822 *
823 * @return string
824 */
825 public function calendar($referenceTime = null, array $formats = [])
826 {
827 /** @var CarbonInterface $current */
828 $current = $this->avoidMutation()->startOfDay();
829 /** @var CarbonInterface $other */
830 $other = $this->resolveCarbon($referenceTime)->avoidMutation()->setTimezone($this->getTimezone())->startOfDay();
831 $diff = $other->diffInDays($current, false);
832 $format = $diff <= -static::DAYS_PER_WEEK ? 'sameElse' : (
833 $diff < -1 ? 'lastWeek' : (
834 $diff < 0 ? 'lastDay' : (
835 $diff < 1 ? 'sameDay' : (
836 $diff < 2 ? 'nextDay' : (
837 $diff < static::DAYS_PER_WEEK ? 'nextWeek' : 'sameElse'
838 )
839 )
840 )
841 )
842 );
843 $format = array_merge($this->getCalendarFormats(), $formats)[$format];
844 if ($format instanceof Closure) {
845 $format = $format($current, $other) ?? '';
846 }
847
848 return $this->isoFormat((string) $format);
849 }
850
851 private function getIntervalDayDiff(DateInterval $interval): int
852 {
853 return (int) $interval->format('%r%a');
854 }
855 }