Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -2158,6 +2158,14 @@ public function toPeriod(...$params): CarbonPeriod

$class = ($params[0] ?? null) instanceof DateTime ? CarbonPeriod::class : CarbonPeriodImmutable::class;

if ($this->step) {
$dates = array_filter($params, static fn (mixed $param) => $param instanceof DateTimeInterface);

if (\count($dates) >= 2 && $dates[0] > $dates[1]) {
$this->invert();
}
}

return $class::create($this, ...$params);
}

Expand Down
16 changes: 1 addition & 15 deletions src/Carbon/CarbonPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -694,20 +694,6 @@ public function __construct(...$arguments)
}
}

if ($raw === null && isset($sortedArguments['start'])) {
$end = $sortedArguments['end'] ?? max(1, $sortedArguments['recurrences'] ?? 1);

if (\is_float($end)) {
$end = $end === INF ? PHP_INT_MAX : (int) round($end);
}

$raw = [
$sortedArguments['start'],
$sortedArguments['interval'] ?? CarbonInterval::day(),
$end,
];
}

$this->setFromAssociativeArray($sortedArguments);

if ($this->startDate === null) {
Expand Down Expand Up @@ -2408,7 +2394,7 @@ protected function filterEndDate(CarbonInterface $current): bool|callable
return true;
}

if ($this->dateInterval->invert ? $current > $this->endDate : $current < $this->endDate) {
if ($this->dateInterval->invert ? ($current > $this->endDate) : ($current < $this->endDate)) {
return true;
}

Expand Down
8 changes: 7 additions & 1 deletion src/Carbon/Traits/Units.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,18 @@ public function add($unit, $value = 1, ?bool $overflow = null): static
$unit = CarbonInterval::make($unit, [], true);
}

$negated = false;

if ($unit instanceof CarbonConverterInterface) {
if ($unit instanceof DateInterval) {
$negated = (bool) $unit->invert;
}

$unit = Closure::fromCallable([$unit, 'convertDate']);
}

if ($unit instanceof Closure) {
$result = $this->resolveCarbon($unit($this, false));
$result = $this->resolveCarbon($unit($this, $negated));

if ($this !== $result && $this->isMutable()) {
return $this->modify($result->rawFormat('Y-m-d H:i:s.u e O'));
Expand Down
55 changes: 55 additions & 0 deletions tests/CarbonPeriod/IteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Tests\CarbonPeriod;

use Carbon\Carbon;
use Carbon\CarbonInterface;
use Carbon\CarbonInterval;
use Carbon\CarbonPeriod;
use Generator;
Expand Down Expand Up @@ -560,4 +561,58 @@ public function testTimezone()

$this->assertSame('00 America/Toronto', $str);
}

public function testDynamicStep()
{
$startDate = Carbon::create('2024-09-5')->startOfDay();
$endDate = Carbon::create('2024-09-12')->startOfDay();

$twoWeekdaysInterval = new CarbonInterval(
fn (CarbonInterface $date, bool $negated): CarbonInterface => $negated
? $date->subWeekdays(2)
: $date->addWeekdays(2),
);
$twoWeekdaysPeriod = $twoWeekdaysInterval->toPeriod($startDate, $endDate);

$this->assertSame(3, $twoWeekdaysPeriod->count());

$dates = [];

foreach ($twoWeekdaysPeriod as $date) {
$dates[] = $date->toDateString();
}

$this->assertSame([
'2024-09-05',
'2024-09-09',
'2024-09-11',
], $dates);
}

public function testReverseOrderWithDynamicStep()
{
$startDate = Carbon::create('2024-09-12')->startOfDay();
$endDate = Carbon::create('2024-09-5')->startOfDay();

$twoWeekdaysInterval = new CarbonInterval(
fn (CarbonInterface $date, bool $negated): CarbonInterface => $negated
? $date->subWeekdays(2)
: $date->addWeekdays(2),
);
$twoWeekdaysPeriod = $twoWeekdaysInterval->toPeriod($startDate, $endDate);

$this->assertSame(3, $twoWeekdaysPeriod->count());

$dates = [];

foreach ($twoWeekdaysPeriod as $date) {
$dates[] = $date->toDateString();
}

$this->assertSame([
'2024-09-12',
'2024-09-10',
'2024-09-06',
], $dates);
}
}
Loading