diff --git a/src/OpeningHours.php b/src/OpeningHours.php index aa5ba51..c834936 100644 --- a/src/OpeningHours.php +++ b/src/OpeningHours.php @@ -525,6 +525,12 @@ public function nextClose( $outputTimezone = $this->getOutputTimezone($dateTime); $dateTime = $this->applyTimezone($dateTime ?? new $this->dateTimeClass()); $dateTime = $this->copyDateTime($dateTime); + $openRangeEnd = $this->currentOpenRange($dateTime)?->end(); + + if ($openRangeEnd && $openRangeEnd->hours() < 24) { + return $openRangeEnd->date() ?? $openRangeEnd->toDateTime($dateTime); + } + $nextClose = null; if ($this->overflow) { @@ -541,7 +547,14 @@ public function nextClose( if (! $nextClose) { $nextClose = $openingHoursForDay->nextClose(PreciseTime::fromDateTime($dateTime)); - if ($nextClose && $nextClose->hours() < 24 && $nextClose->format('Gi') < $dateTime->format('Gi')) { + if ( + $nextClose + && $nextClose->hours() < 24 + && ( + $nextClose->format('Gi') < $dateTime->format('Gi') + || ($this->isClosedAt($dateTime) && $this->nextOpen($dateTime)->format('Gi') > $nextClose->format('Gi')) + ) + ) { $dateTime = $dateTime->modify('+1 day'); } } diff --git a/src/Time.php b/src/Time.php index 8c2c903..d7f6496 100644 --- a/src/Time.php +++ b/src/Time.php @@ -45,7 +45,7 @@ public function minutes(): int return $this->minutes; } - public function date(): DateTimeInterface + public function date(): ?DateTimeInterface { return $this->date; } diff --git a/tests/OpeningHoursOverflowTest.php b/tests/OpeningHoursOverflowTest.php index 67ec7dd..530cb59 100644 --- a/tests/OpeningHoursOverflowTest.php +++ b/tests/OpeningHoursOverflowTest.php @@ -195,12 +195,28 @@ public function overflow_next_close() $this->assertSame('2024-11-13 06:00', $nextClose->format('Y-m-d H:i')); + $nextClose = $openingHours->nextClose(new DateTime('2024-11-12 05:30:00')); + + $openingHours = OpeningHours::create([ + 'overflow' => true, + 'monday' => ['18:00-05:00'], // 2024-11-11 + 'tuesday' => ['05:40-05:50', '17:00-06:00'], // 2024-11-12 + ]); + + $nextClose = $openingHours->nextClose(new DateTime('2024-11-12 05:30:00')); + + $this->assertSame('2024-11-12 05:50', $nextClose->format('Y-m-d H:i')); + $openingHours = OpeningHours::create([ 'overflow' => true, 'monday' => ['18:00-22:00', '23:00-05:00'], // 2024-11-11 'tuesday' => ['17:00-06:00'], // 2024-11-12 ]); + $nextClose = $openingHours->nextClose(new DateTime('2024-11-11 23:30:00')); + + $this->assertSame('2024-11-12 05:00', $nextClose->format('Y-m-d H:i')); + $nextClose = $openingHours->nextClose(new DateTime('2024-11-12 04:00:00')); $this->assertSame('2024-11-12 05:00', $nextClose->format('Y-m-d H:i'));