0

I am trying to build a CarbonPeriod and CarbonInterval to return a set of occurrences. I am running into month overflow. Take the following example:

$startDate = Carbon::parse('2022-01-20');
$endDate = Carbon::parse('2022-06-14');
$separation_count = 1;
$day_of_month = 30;

$datePeriod = CarbonPeriod::create($startDate, $endDate)
    ->settings(['monthOverflow' => false]);
$dateInterval = CarbonInterval::months($separation_count);

if ($day_of_month) {
    $datePeriod->setStartDate(
        $startDate->copy()->setDay($day_of_month)
    );
    $dayFilter = function (Carbon $date) use ($day_of_month) {
        return $date->day === $day_of_month;
    };
    $datePeriod->addFilter($dayFilter);
}
$datePeriod->setDateInterval($dateInterval);
return $datePeriod;

The CarbonInterval months is returning the following dates:

  1. 2022-01-30

I know on Carbon instances you can call addMonthNoOverflow to address this issue. How do you do it in intervals and periods?

I would like the CarbonPeriod to return

  1. 2022-01-30
  2. 2022-03-30
  3. 2022-04-30
  4. 2022-05-30
Jon Erickson
  • 1,876
  • 4
  • 30
  • 73
  • I edited your question to make it a [mre]; running `foreach ($datePeriod as $d) echo $d->toDateString();` I get the desired values. – miken32 Jan 27 '22 at 20:29
  • Thank you, I get the same as well. I updated the code to reflect my issue. When setting $separation_count to 1, I do not get every month. – Jon Erickson Jan 27 '22 at 20:48
  • Ah, February is causing the problem (as usual!) Does this answer your question? [Find dates between 2 dates with custom interval and without overflow](https://stackoverflow.com/questions/62970150/find-dates-between-2-dates-with-custom-interval-and-without-overflow) – miken32 Jan 27 '22 at 22:26

0 Answers0