-1

I am trying to determine the difference in months between the date the user is created and the current date.

However, if any of the date involves february it will result in the difference being returned to potentially be wrong.

Instead of returning difference of 1 it is returning 0.

Code

   $associated_user_created_at = Carbon::parse("2023-02-01 00:00:00");
    $currentDate = Carbon::parse("2023-03-01 23:59:59");
    error_log("H E L L O ");
    error_log($associated_user_created_at->format('d F Y'));
    error_log($currentDate->format('d F Y'));
    $diffInMonths = $associated_user_created_at->diffInMonths($currentDate, false);
    // $diffInMonths = $associated_user_created_at->diff($currentDate);
    error_log("****");
    error_log("Diff in months = " .$diffInMonths);
    dd($diffInMonths);

Output

H E L L O
01 February 2023
01 March 2023
****
Diff in months = 0

I have tried following the suggested solutions in this thread Incorrect diffInMonths laravel Carbon between Feb 1 and Mar 1 however it doesn't solve the issue.

Project specifications

  • Php 7.4.25
  • Laravel 5
Yeo Bryan
  • 331
  • 4
  • 24
  • 1
    Keep in mind that Laravel 5 is horribly outdated! Are you sure this is related to Laravel after all? What do all these `error_log` lines print? – Nico Haase Jul 05 '23 at 07:44
  • @NicoHaase I have edited the post to include the output – Yeo Bryan Jul 05 '23 at 07:52
  • 1
    Tough subject, as there is many ways to calculate month diff, each being correct or not depending on the use case https://github.com/briannesbitt/Carbon/issues/2604 – KyleK Jul 05 '23 at 08:08
  • And then what is your version of Carbon? It's probably outdated. IIRC Laravel 5 is stuck with Carbon v1 – KyleK Jul 05 '23 at 08:10
  • This is likely a carbon or php version bug because on PHP 8.2 and Laravel 10 (using Carbon 2+) this works as expected. If this is the only wrong case you can probably hardcode the value – apokryfos Jul 05 '23 at 08:59

2 Answers2

1

Let's assume you should not use Carbon::diffInMonths() for that because your version is outdated. Then for anniversary-style month diff, you can:

$year = $associated_user_created_at->year;
$month = $associated_user_created_at->month;
$day = $associated_user_created_at->day;
$diff = ($currentDate->year - $year) * 12 + $currentDate->month - $month - ($currentDate->day < $day ? 1 : 0);

(Assuming currentDate >= associated_user_created_at)

KyleK
  • 4,643
  • 17
  • 33
  • Thanks! It actually is returning the expected output, but can you elaborate on why diffInMonths isn't working for me? And what do you mean by anniversary style month diff – Yeo Bryan Jul 05 '23 at 08:24
  • When using your code with the last version of Carbon, the diff is `1` and diffInMonth() get reworked many times so I assume you get `0` because you use an old version. Anniversary-style is the calculation of the diff from start then counting +1 every time you reach the same day number on next month (of full month if they don't contain this day number) See https://github.com/briannesbitt/Carbon/issues/2604 – KyleK Jul 05 '23 at 08:57
-1

You can use this solution to get the number of different month using Carbon diffnInMonth

$associated_user_created_at = Carbon::parse("2023-02-01 00:00:00")->format('Y-m-d');
$currentDate = Carbon::parse("2023-03-01 23:59:59")->format('Y-m-d');
$monthDiff = Carbon::parse($currentDate)->diffInMonths(Carbon::parse($associated_user_created_at));
dd($monthDiff);
Parth_009
  • 61
  • 5