I found a weird result when I use diffInMonths of laravel Carbon. Can anyone explain for me why it works like this. Is it a bug? And how to get it correct.
$d1 = new Carbon('2018-02-01');
$d2 = new Carbon('2018-03-01');
dd($d1->diffInMonths($d2));
Output was: 0 (Expected: 1)
BUT
$d1 = new Carbon('2018-02-02');
$d2 = new Carbon('2018-03-02');
dd($d1->diffInMonths($d2));
Output was: 1
My setting timezone is "Asia/Ho_Chi_Minh" (GMT +7). PHP Version 7.0.22. OS: Ubuntu Server 16.04
This weird result happends only between February 1 and March 1, other months result as expected.
I also try date_diff
instead of Carbon
$d1 = new \DateTime('2018-02-01');
$d2 = new \DateTime('2018-03-01');
$diff = date_diff($d1,$d2);
dump($diff->m);
dump($diff->d);
die;
It returns months: 0, and days: 28 (Not month: 1, days: 0 as expected)
So the problem may be from PHP not from Carbon.