Boundaries.php
4 years ago
Cast.php
4 years ago
Comparison.php
2 years ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
8 months ago
DeprecatedProperties.php
4 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
4 years ago
Localization.php
8 months ago
Macro.php
4 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
4 years ago
Mutability.php
4 years ago
ObjectInitialisation.php
4 years ago
Options.php
2 years ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
8 months ago
Timestamp.php
1 year ago
ToStringFormat.php
3 years ago
Units.php
2 years ago
Week.php
4 years ago
index.php
3 years ago
Difference.php
426 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Carbon\Traits; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Carbon\Carbon; |
| 5 | use MailPoetVendor\Carbon\CarbonImmutable; |
| 6 | use MailPoetVendor\Carbon\CarbonInterface; |
| 7 | use MailPoetVendor\Carbon\CarbonInterval; |
| 8 | use MailPoetVendor\Carbon\CarbonPeriod; |
| 9 | use MailPoetVendor\Carbon\Translator; |
| 10 | use Closure; |
| 11 | use DateInterval; |
| 12 | use DateTimeInterface; |
| 13 | use ReturnTypeWillChange; |
| 14 | trait Difference |
| 15 | { |
| 16 | protected static function fixNegativeMicroseconds(CarbonInterval $diff) |
| 17 | { |
| 18 | if ($diff->s !== 0 || $diff->i !== 0 || $diff->h !== 0 || $diff->d !== 0 || $diff->m !== 0 || $diff->y !== 0) { |
| 19 | $diff->f = (\round($diff->f * 1000000) + 1000000) / 1000000; |
| 20 | $diff->s--; |
| 21 | if ($diff->s < 0) { |
| 22 | $diff->s += 60; |
| 23 | $diff->i--; |
| 24 | if ($diff->i < 0) { |
| 25 | $diff->i += 60; |
| 26 | $diff->h--; |
| 27 | if ($diff->h < 0) { |
| 28 | $diff->h += 24; |
| 29 | $diff->d--; |
| 30 | if ($diff->d < 0) { |
| 31 | $diff->d += 30; |
| 32 | $diff->m--; |
| 33 | if ($diff->m < 0) { |
| 34 | $diff->m += 12; |
| 35 | $diff->y--; |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | return; |
| 42 | } |
| 43 | $diff->f *= -1; |
| 44 | $diff->invert(); |
| 45 | } |
| 46 | protected static function fixDiffInterval(DateInterval $diff, $absolute, array $skip = []) |
| 47 | { |
| 48 | $diff = CarbonInterval::instance($diff, $skip); |
| 49 | // Work-around for https://bugs.php.net/bug.php?id=77145 |
| 50 | // @codeCoverageIgnoreStart |
| 51 | if ($diff->f > 0 && $diff->y === -1 && $diff->m === 11 && $diff->d >= 27 && $diff->h === 23 && $diff->i === 59 && $diff->s === 59) { |
| 52 | $diff->y = 0; |
| 53 | $diff->m = 0; |
| 54 | $diff->d = 0; |
| 55 | $diff->h = 0; |
| 56 | $diff->i = 0; |
| 57 | $diff->s = 0; |
| 58 | $diff->f = (1000000 - \round($diff->f * 1000000)) / 1000000; |
| 59 | $diff->invert(); |
| 60 | } elseif ($diff->f < 0) { |
| 61 | static::fixNegativeMicroseconds($diff); |
| 62 | } |
| 63 | // @codeCoverageIgnoreEnd |
| 64 | if ($absolute && $diff->invert) { |
| 65 | $diff->invert(); |
| 66 | } |
| 67 | return $diff; |
| 68 | } |
| 69 | #[\ReturnTypeWillChange] |
| 70 | public function diff($date = null, $absolute = \false) |
| 71 | { |
| 72 | $other = $this->resolveCarbon($date); |
| 73 | // Work-around for https://bugs.php.net/bug.php?id=81458 |
| 74 | // It was initially introduced for https://bugs.php.net/bug.php?id=80998 |
| 75 | // The very specific case of 80998 was fixed in PHP 8.1beta3, but it introduced 81458 |
| 76 | // So we still need to keep this for now |
| 77 | // @codeCoverageIgnoreStart |
| 78 | if (\version_compare(\PHP_VERSION, '8.1.0-dev', '>=') && $other->tz !== $this->tz) { |
| 79 | $other = $other->avoidMutation()->tz($this->tz); |
| 80 | } |
| 81 | // @codeCoverageIgnoreEnd |
| 82 | return parent::diff($other, (bool) $absolute); |
| 83 | } |
| 84 | public function diffAsCarbonInterval($date = null, $absolute = \true, array $skip = []) |
| 85 | { |
| 86 | return static::fixDiffInterval($this->diff($this->resolveCarbon($date), $absolute), $absolute, $skip); |
| 87 | } |
| 88 | public function diffInYears($date = null, $absolute = \true) |
| 89 | { |
| 90 | return (int) $this->diff($this->resolveCarbon($date), $absolute)->format('%r%y'); |
| 91 | } |
| 92 | public function diffInQuarters($date = null, $absolute = \true) |
| 93 | { |
| 94 | return (int) ($this->diffInMonths($date, $absolute) / static::MONTHS_PER_QUARTER); |
| 95 | } |
| 96 | public function diffInMonths($date = null, $absolute = \true) |
| 97 | { |
| 98 | $date = $this->resolveCarbon($date)->avoidMutation()->tz($this->tz); |
| 99 | [$yearStart, $monthStart, $dayStart] = \explode('-', $this->format('Y-m-dHisu')); |
| 100 | [$yearEnd, $monthEnd, $dayEnd] = \explode('-', $date->format('Y-m-dHisu')); |
| 101 | $diff = ((int) $yearEnd - (int) $yearStart) * static::MONTHS_PER_YEAR + (int) $monthEnd - (int) $monthStart; |
| 102 | if ($diff > 0) { |
| 103 | $diff -= $dayStart > $dayEnd ? 1 : 0; |
| 104 | } elseif ($diff < 0) { |
| 105 | $diff += $dayStart < $dayEnd ? 1 : 0; |
| 106 | } |
| 107 | return $absolute ? \abs($diff) : $diff; |
| 108 | } |
| 109 | public function diffInWeeks($date = null, $absolute = \true) |
| 110 | { |
| 111 | return (int) ($this->diffInDays($date, $absolute) / static::DAYS_PER_WEEK); |
| 112 | } |
| 113 | public function diffInDays($date = null, $absolute = \true) |
| 114 | { |
| 115 | return $this->getIntervalDayDiff($this->diff($this->resolveCarbon($date), $absolute)); |
| 116 | } |
| 117 | public function diffInDaysFiltered(Closure $callback, $date = null, $absolute = \true) |
| 118 | { |
| 119 | return $this->diffFiltered(CarbonInterval::day(), $callback, $date, $absolute); |
| 120 | } |
| 121 | public function diffInHoursFiltered(Closure $callback, $date = null, $absolute = \true) |
| 122 | { |
| 123 | return $this->diffFiltered(CarbonInterval::hour(), $callback, $date, $absolute); |
| 124 | } |
| 125 | public function diffFiltered(CarbonInterval $ci, Closure $callback, $date = null, $absolute = \true) |
| 126 | { |
| 127 | $start = $this; |
| 128 | $end = $this->resolveCarbon($date); |
| 129 | $inverse = \false; |
| 130 | if ($end < $start) { |
| 131 | $start = $end; |
| 132 | $end = $this; |
| 133 | $inverse = \true; |
| 134 | } |
| 135 | $options = CarbonPeriod::EXCLUDE_END_DATE | ($this->isMutable() ? 0 : CarbonPeriod::IMMUTABLE); |
| 136 | $diff = $ci->toPeriod($start, $end, $options)->filter($callback)->count(); |
| 137 | return $inverse && !$absolute ? -$diff : $diff; |
| 138 | } |
| 139 | public function diffInWeekdays($date = null, $absolute = \true) |
| 140 | { |
| 141 | return $this->diffInDaysFiltered(static function (CarbonInterface $date) { |
| 142 | return $date->isWeekday(); |
| 143 | }, $this->resolveCarbon($date)->avoidMutation()->modify($this->format('H:i:s.u')), $absolute); |
| 144 | } |
| 145 | public function diffInWeekendDays($date = null, $absolute = \true) |
| 146 | { |
| 147 | return $this->diffInDaysFiltered(static function (CarbonInterface $date) { |
| 148 | return $date->isWeekend(); |
| 149 | }, $this->resolveCarbon($date)->avoidMutation()->modify($this->format('H:i:s.u')), $absolute); |
| 150 | } |
| 151 | public function diffInHours($date = null, $absolute = \true) |
| 152 | { |
| 153 | return (int) ($this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR); |
| 154 | } |
| 155 | public function diffInRealHours($date = null, $absolute = \true) |
| 156 | { |
| 157 | return (int) ($this->diffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE / static::MINUTES_PER_HOUR); |
| 158 | } |
| 159 | public function diffInMinutes($date = null, $absolute = \true) |
| 160 | { |
| 161 | return (int) ($this->diffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE); |
| 162 | } |
| 163 | public function diffInRealMinutes($date = null, $absolute = \true) |
| 164 | { |
| 165 | return (int) ($this->diffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE); |
| 166 | } |
| 167 | public function diffInSeconds($date = null, $absolute = \true) |
| 168 | { |
| 169 | $diff = $this->diff($date); |
| 170 | if ($diff->days === 0) { |
| 171 | $diff = static::fixDiffInterval($diff, $absolute); |
| 172 | } |
| 173 | $value = ((($diff->m || $diff->y ? $diff->days : $diff->d) * static::HOURS_PER_DAY + $diff->h) * static::MINUTES_PER_HOUR + $diff->i) * static::SECONDS_PER_MINUTE + $diff->s; |
| 174 | return $absolute || !$diff->invert ? $value : -$value; |
| 175 | } |
| 176 | public function diffInMicroseconds($date = null, $absolute = \true) |
| 177 | { |
| 178 | $diff = $this->diff($date); |
| 179 | $value = (int) \round((((($diff->m || $diff->y ? $diff->days : $diff->d) * static::HOURS_PER_DAY + $diff->h) * static::MINUTES_PER_HOUR + $diff->i) * static::SECONDS_PER_MINUTE + ($diff->f + $diff->s)) * static::MICROSECONDS_PER_SECOND); |
| 180 | return $absolute || !$diff->invert ? $value : -$value; |
| 181 | } |
| 182 | public function diffInMilliseconds($date = null, $absolute = \true) |
| 183 | { |
| 184 | return (int) ($this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND); |
| 185 | } |
| 186 | public function diffInRealSeconds($date = null, $absolute = \true) |
| 187 | { |
| 188 | $date = $this->resolveCarbon($date); |
| 189 | $value = $date->getTimestamp() - $this->getTimestamp(); |
| 190 | return $absolute ? \abs($value) : $value; |
| 191 | } |
| 192 | public function diffInRealMicroseconds($date = null, $absolute = \true) |
| 193 | { |
| 194 | $date = $this->resolveCarbon($date); |
| 195 | $value = ($date->timestamp - $this->timestamp) * static::MICROSECONDS_PER_SECOND + $date->micro - $this->micro; |
| 196 | return $absolute ? \abs($value) : $value; |
| 197 | } |
| 198 | public function diffInRealMilliseconds($date = null, $absolute = \true) |
| 199 | { |
| 200 | return (int) ($this->diffInRealMicroseconds($date, $absolute) / static::MICROSECONDS_PER_MILLISECOND); |
| 201 | } |
| 202 | public function floatDiffInSeconds($date = null, $absolute = \true) |
| 203 | { |
| 204 | return (float) ($this->diffInMicroseconds($date, $absolute) / static::MICROSECONDS_PER_SECOND); |
| 205 | } |
| 206 | public function floatDiffInMinutes($date = null, $absolute = \true) |
| 207 | { |
| 208 | return $this->floatDiffInSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; |
| 209 | } |
| 210 | public function floatDiffInHours($date = null, $absolute = \true) |
| 211 | { |
| 212 | return $this->floatDiffInMinutes($date, $absolute) / static::MINUTES_PER_HOUR; |
| 213 | } |
| 214 | public function floatDiffInDays($date = null, $absolute = \true) |
| 215 | { |
| 216 | $hoursDiff = $this->floatDiffInHours($date, $absolute); |
| 217 | $interval = $this->diff($date, $absolute); |
| 218 | if ($interval->y === 0 && $interval->m === 0 && $interval->d === 0) { |
| 219 | return $hoursDiff / static::HOURS_PER_DAY; |
| 220 | } |
| 221 | $daysDiff = $this->getIntervalDayDiff($interval); |
| 222 | return $daysDiff + \fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; |
| 223 | } |
| 224 | public function floatDiffInWeeks($date = null, $absolute = \true) |
| 225 | { |
| 226 | return $this->floatDiffInDays($date, $absolute) / static::DAYS_PER_WEEK; |
| 227 | } |
| 228 | public function floatDiffInMonths($date = null, $absolute = \true) |
| 229 | { |
| 230 | $start = $this; |
| 231 | $end = $this->resolveCarbon($date); |
| 232 | $ascending = $start <= $end; |
| 233 | $sign = $absolute || $ascending ? 1 : -1; |
| 234 | if (!$ascending) { |
| 235 | [$start, $end] = [$end, $start]; |
| 236 | } |
| 237 | $monthsDiff = $start->diffInMonths($end); |
| 238 | $floorEnd = $start->avoidMutation()->addMonths($monthsDiff); |
| 239 | if ($floorEnd >= $end) { |
| 240 | return $sign * $monthsDiff; |
| 241 | } |
| 242 | $startOfMonthAfterFloorEnd = $floorEnd->avoidMutation()->addMonth()->startOfMonth(); |
| 243 | if ($startOfMonthAfterFloorEnd > $end) { |
| 244 | return $sign * ($monthsDiff + $floorEnd->floatDiffInDays($end) / $floorEnd->daysInMonth); |
| 245 | } |
| 246 | return $sign * ($monthsDiff + $floorEnd->floatDiffInDays($startOfMonthAfterFloorEnd) / $floorEnd->daysInMonth + $startOfMonthAfterFloorEnd->floatDiffInDays($end) / $end->daysInMonth); |
| 247 | } |
| 248 | public function floatDiffInYears($date = null, $absolute = \true) |
| 249 | { |
| 250 | $start = $this; |
| 251 | $end = $this->resolveCarbon($date); |
| 252 | $ascending = $start <= $end; |
| 253 | $sign = $absolute || $ascending ? 1 : -1; |
| 254 | if (!$ascending) { |
| 255 | [$start, $end] = [$end, $start]; |
| 256 | } |
| 257 | $yearsDiff = $start->diffInYears($end); |
| 258 | $floorEnd = $start->avoidMutation()->addYears($yearsDiff); |
| 259 | if ($floorEnd >= $end) { |
| 260 | return $sign * $yearsDiff; |
| 261 | } |
| 262 | $startOfYearAfterFloorEnd = $floorEnd->avoidMutation()->addYear()->startOfYear(); |
| 263 | if ($startOfYearAfterFloorEnd > $end) { |
| 264 | return $sign * ($yearsDiff + $floorEnd->floatDiffInDays($end) / $floorEnd->daysInYear); |
| 265 | } |
| 266 | return $sign * ($yearsDiff + $floorEnd->floatDiffInDays($startOfYearAfterFloorEnd) / $floorEnd->daysInYear + $startOfYearAfterFloorEnd->floatDiffInDays($end) / $end->daysInYear); |
| 267 | } |
| 268 | public function floatDiffInRealSeconds($date = null, $absolute = \true) |
| 269 | { |
| 270 | return $this->diffInRealMicroseconds($date, $absolute) / static::MICROSECONDS_PER_SECOND; |
| 271 | } |
| 272 | public function floatDiffInRealMinutes($date = null, $absolute = \true) |
| 273 | { |
| 274 | return $this->floatDiffInRealSeconds($date, $absolute) / static::SECONDS_PER_MINUTE; |
| 275 | } |
| 276 | public function floatDiffInRealHours($date = null, $absolute = \true) |
| 277 | { |
| 278 | return $this->floatDiffInRealMinutes($date, $absolute) / static::MINUTES_PER_HOUR; |
| 279 | } |
| 280 | public function floatDiffInRealDays($date = null, $absolute = \true) |
| 281 | { |
| 282 | $date = $this->resolveUTC($date); |
| 283 | $utc = $this->avoidMutation()->utc(); |
| 284 | $hoursDiff = $utc->floatDiffInRealHours($date, $absolute); |
| 285 | return ($hoursDiff < 0 ? -1 : 1) * $utc->diffInDays($date) + \fmod($hoursDiff, static::HOURS_PER_DAY) / static::HOURS_PER_DAY; |
| 286 | } |
| 287 | public function floatDiffInRealWeeks($date = null, $absolute = \true) |
| 288 | { |
| 289 | return $this->floatDiffInRealDays($date, $absolute) / static::DAYS_PER_WEEK; |
| 290 | } |
| 291 | public function floatDiffInRealMonths($date = null, $absolute = \true) |
| 292 | { |
| 293 | $start = $this; |
| 294 | $end = $this->resolveCarbon($date); |
| 295 | $ascending = $start <= $end; |
| 296 | $sign = $absolute || $ascending ? 1 : -1; |
| 297 | if (!$ascending) { |
| 298 | [$start, $end] = [$end, $start]; |
| 299 | } |
| 300 | $monthsDiff = $start->diffInMonths($end); |
| 301 | $floorEnd = $start->avoidMutation()->addMonths($monthsDiff); |
| 302 | if ($floorEnd >= $end) { |
| 303 | return $sign * $monthsDiff; |
| 304 | } |
| 305 | $startOfMonthAfterFloorEnd = $floorEnd->avoidMutation()->addMonth()->startOfMonth(); |
| 306 | if ($startOfMonthAfterFloorEnd > $end) { |
| 307 | return $sign * ($monthsDiff + $floorEnd->floatDiffInRealDays($end) / $floorEnd->daysInMonth); |
| 308 | } |
| 309 | return $sign * ($monthsDiff + $floorEnd->floatDiffInRealDays($startOfMonthAfterFloorEnd) / $floorEnd->daysInMonth + $startOfMonthAfterFloorEnd->floatDiffInRealDays($end) / $end->daysInMonth); |
| 310 | } |
| 311 | public function floatDiffInRealYears($date = null, $absolute = \true) |
| 312 | { |
| 313 | $start = $this; |
| 314 | $end = $this->resolveCarbon($date); |
| 315 | $ascending = $start <= $end; |
| 316 | $sign = $absolute || $ascending ? 1 : -1; |
| 317 | if (!$ascending) { |
| 318 | [$start, $end] = [$end, $start]; |
| 319 | } |
| 320 | $yearsDiff = $start->diffInYears($end); |
| 321 | $floorEnd = $start->avoidMutation()->addYears($yearsDiff); |
| 322 | if ($floorEnd >= $end) { |
| 323 | return $sign * $yearsDiff; |
| 324 | } |
| 325 | $startOfYearAfterFloorEnd = $floorEnd->avoidMutation()->addYear()->startOfYear(); |
| 326 | if ($startOfYearAfterFloorEnd > $end) { |
| 327 | return $sign * ($yearsDiff + $floorEnd->floatDiffInRealDays($end) / $floorEnd->daysInYear); |
| 328 | } |
| 329 | return $sign * ($yearsDiff + $floorEnd->floatDiffInRealDays($startOfYearAfterFloorEnd) / $floorEnd->daysInYear + $startOfYearAfterFloorEnd->floatDiffInRealDays($end) / $end->daysInYear); |
| 330 | } |
| 331 | public function secondsSinceMidnight() |
| 332 | { |
| 333 | return $this->diffInSeconds($this->avoidMutation()->startOfDay()); |
| 334 | } |
| 335 | public function secondsUntilEndOfDay() |
| 336 | { |
| 337 | return $this->diffInSeconds($this->avoidMutation()->endOfDay()); |
| 338 | } |
| 339 | public function diffForHumans($other = null, $syntax = null, $short = \false, $parts = 1, $options = null) |
| 340 | { |
| 341 | if (\is_array($other)) { |
| 342 | $other['syntax'] = \array_key_exists('syntax', $other) ? $other['syntax'] : $syntax; |
| 343 | $syntax = $other; |
| 344 | $other = $syntax['other'] ?? null; |
| 345 | } |
| 346 | $intSyntax =& $syntax; |
| 347 | if (\is_array($syntax)) { |
| 348 | $syntax['syntax'] = $syntax['syntax'] ?? null; |
| 349 | $intSyntax =& $syntax['syntax']; |
| 350 | } |
| 351 | $intSyntax = (int) ($intSyntax ?? static::DIFF_RELATIVE_AUTO); |
| 352 | $intSyntax = $intSyntax === static::DIFF_RELATIVE_AUTO && $other === null ? static::DIFF_RELATIVE_TO_NOW : $intSyntax; |
| 353 | $parts = \min(7, \max(1, (int) $parts)); |
| 354 | $skip = \is_array($syntax) ? $syntax['skip'] ?? [] : []; |
| 355 | return $this->diffAsCarbonInterval($other, \false, (array) $skip)->setLocalTranslator($this->getLocalTranslator())->forHumans($syntax, (bool) $short, $parts, $options ?? $this->localHumanDiffOptions ?? static::getHumanDiffOptions()); |
| 356 | } |
| 357 | public function from($other = null, $syntax = null, $short = \false, $parts = 1, $options = null) |
| 358 | { |
| 359 | return $this->diffForHumans($other, $syntax, $short, $parts, $options); |
| 360 | } |
| 361 | public function since($other = null, $syntax = null, $short = \false, $parts = 1, $options = null) |
| 362 | { |
| 363 | return $this->diffForHumans($other, $syntax, $short, $parts, $options); |
| 364 | } |
| 365 | public function to($other = null, $syntax = null, $short = \false, $parts = 1, $options = null) |
| 366 | { |
| 367 | if (!$syntax && !$other) { |
| 368 | $syntax = CarbonInterface::DIFF_RELATIVE_TO_NOW; |
| 369 | } |
| 370 | return $this->resolveCarbon($other)->diffForHumans($this, $syntax, $short, $parts, $options); |
| 371 | } |
| 372 | public function until($other = null, $syntax = null, $short = \false, $parts = 1, $options = null) |
| 373 | { |
| 374 | return $this->to($other, $syntax, $short, $parts, $options); |
| 375 | } |
| 376 | public function fromNow($syntax = null, $short = \false, $parts = 1, $options = null) |
| 377 | { |
| 378 | $other = null; |
| 379 | if ($syntax instanceof DateTimeInterface) { |
| 380 | [$other, $syntax, $short, $parts, $options] = \array_pad(\func_get_args(), 5, null); |
| 381 | } |
| 382 | return $this->from($other, $syntax, $short, $parts, $options); |
| 383 | } |
| 384 | public function toNow($syntax = null, $short = \false, $parts = 1, $options = null) |
| 385 | { |
| 386 | return $this->to(null, $syntax, $short, $parts, $options); |
| 387 | } |
| 388 | public function ago($syntax = null, $short = \false, $parts = 1, $options = null) |
| 389 | { |
| 390 | $other = null; |
| 391 | if ($syntax instanceof DateTimeInterface) { |
| 392 | [$other, $syntax, $short, $parts, $options] = \array_pad(\func_get_args(), 5, null); |
| 393 | } |
| 394 | return $this->from($other, $syntax, $short, $parts, $options); |
| 395 | } |
| 396 | public function timespan($other = null, $timezone = null) |
| 397 | { |
| 398 | if (!$other instanceof DateTimeInterface) { |
| 399 | $other = static::parse($other, $timezone); |
| 400 | } |
| 401 | return $this->diffForHumans($other, ['join' => ', ', 'syntax' => CarbonInterface::DIFF_ABSOLUTE, 'options' => CarbonInterface::NO_ZERO_DIFF, 'parts' => -1]); |
| 402 | } |
| 403 | public function calendar($referenceTime = null, array $formats = []) |
| 404 | { |
| 405 | $current = $this->avoidMutation()->startOfDay(); |
| 406 | $other = $this->resolveCarbon($referenceTime)->avoidMutation()->setTimezone($this->getTimezone())->startOfDay(); |
| 407 | $diff = $other->diffInDays($current, \false); |
| 408 | $format = $diff < -6 ? 'sameElse' : ($diff < -1 ? 'lastWeek' : ($diff < 0 ? 'lastDay' : ($diff < 1 ? 'sameDay' : ($diff < 2 ? 'nextDay' : ($diff < 7 ? 'nextWeek' : 'sameElse'))))); |
| 409 | $format = \array_merge($this->getCalendarFormats(), $formats)[$format]; |
| 410 | if ($format instanceof Closure) { |
| 411 | $format = $format($current, $other) ?? ''; |
| 412 | } |
| 413 | return $this->isoFormat((string) $format); |
| 414 | } |
| 415 | private function getIntervalDayDiff(DateInterval $interval) : int |
| 416 | { |
| 417 | $daysDiff = (int) $interval->format('%a'); |
| 418 | $sign = $interval->format('%r') === '-' ? -1 : 1; |
| 419 | if (\is_int($interval->days) && $interval->y === 0 && $interval->m === 0 && \version_compare(\PHP_VERSION, '8.1.0-dev', '<') && \abs($interval->d - $daysDiff) === 1) { |
| 420 | $daysDiff = \abs($interval->d); |
| 421 | // @codeCoverageIgnore |
| 422 | } |
| 423 | return $daysDiff * $sign; |
| 424 | } |
| 425 | } |
| 426 |