i update all my project built in laravel.
In any part of this project i have this code: $FI->copy()->addMonth(3)->subDay();
but when pass phpStan
return
Method CarbonCarbon::addMonth() invoked with 1 parameter, 0
required.
I´m reading and now i have to change to this method $FI->copy()->addMonthsWithOverflow(3)->subDay();
But i don´t know if this code it´s same that previous code.. I´m building script to try check if both dates it´s same:
$period = CarbonPeriod::create('2023-12-31T00:00:00Z', '2024-05-31T00:00:00Z');
// Iterate over the period
$dates = array();
foreach ($period as $date) {
$date->format('Y-m-dTH:i:sZ');
$final_date = $date->copy()->addMonth(3)->subDay();
array_push($dates, $final_date);
}
foreach ($dates as $key => $value){
echo "key ".$key." value: ".$value."<br>";
}
echo "<br>";
$dates2 = array();
foreach ($period as $date) {
$date->format('Y-m-dTH:i:sZ');
$final_date = $date->copy()->addMonthsWithOverflow(3)->subDay();
array_push($dates2, $final_date);
}
foreach ($dates as $key => $value){
echo "key2 ".$key." value2: ".$value."<br>";
}
i can show that both dates it´s same, but i need be sure that this new method it´s correct.
Thanks for readme and sorry for my bad english
2
Answers
In Carbon there is a difference between
addMonth()
andaddMonths()
.addMonth()
is a shorthand foraddMonths(1)
, if you want to invoke the method with a parameter, as you are doing right now, it should beaddMonths(3)
instead ofaddMonth()
.To address the confusion you’re facing with
addMonth()
andaddMonthsWithOverflow()
I can say:The behaviors of the two methods,
addMonth()
andaddMonthsWithOverflow()
, in Carbon are somewhat different when the current day of the month goes beyond the maximum day of the month being added. Specifically, theaddMonth()
method will change the day to the last day of the new month, whereas theaddMonthsWithOverflow()
method will retain the current day and flow over to the next month.However the error you’re facing will be fixed following @mmrhn answer.