skip to Main Content

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


  1. In Carbon there is a difference between addMonth() and addMonths(). addMonth() is a shorthand for addMonths(1), if you want to invoke the method with a parameter, as you are doing right now, it should be addMonths(3) instead of addMonth().

    Login or Signup to reply.
  2. To address the confusion you’re facing with addMonth() and addMonthsWithOverflow() I can say:

    The behaviors of the two methods, addMonth() and addMonthsWithOverflow(), in Carbon are somewhat different when the current day of the month goes beyond the maximum day of the month being added. Specifically, the addMonth() method will change the day to the last day of the new month, whereas the addMonthsWithOverflow() 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search