Search code examples
phpphp-carbon

why does Carbon::parse('2023-11-30')->diffInMonths(Carbon::parse('2023-10-01'), false); return 1 instead of 2?


When I try to use carbon diffInMonths() it gives me values less 1, why is that?

$months = Carbon::parse('2023-11-30')->diffInMonths(Carbon::parse('2023-10-01'), false);

//$months = 1 instead of 2

What am I doing wrong?

I am manually adding 1 after results which is dangerous.


Solution

  • If you want rounded diff:

    $months = round(Carbon::parse('2023-11-30')->floatDiffInMonths(Carbon::parse('2023-10-01'), false));