I have a customisable DateInterval object, along with a customisable start and end date. I want to find the dates between the start and end using the interval. I am using Carbon to try and help with this.
Here's the problem:
I the interval is months but the start date is > 28 I cannot control the overflow using CarbonPeriod
.
Here is the code I am testing with:
$di = CarbonInterval::create('P1M');
$start = Carbon::parse('31 january 2020')->startOfDay();
$end = Carbon::parse('01 april 2020')->startOfDay();
$period = CarbonPeriod::create($start, $di, $end);
$items = [];
foreach ($period as $item) {
$items[] = $item;
}
I want the above to result in
2020-01-31
2020-02-29
2020-03-30
But I get
2020-01-31
2020-03-02
2020-04-02
Remember, the DateInterval is customisable (or I would just use Carbon::addMonthNoOverflow()
).
Can anyone please help with how I achieve what I need to, above?