PHP Carbon diffInMonths

I have a problem with PHP Carbon diffInMonths function. In some cases, I got the wrong result. For example:

$start = \Carbon\Carbon::create(2017, 4, 1);
$end   = \Carbon\Carbon::create(2017, 5, 1);

echo $start->diffInMonths($end);

      

I should get 1, but I got 0. I am using PHP 7.1 and Laravel 5.4.

Does anyone have one problem? How can I fix this? Thanks for the help!

+3


source to share


1 answer


As the Carbon issue on github says it is a bug. There is more here .

<?php
$d1 = new DateTime("2015-03-01 00:00:00.000000", new DateTimeZone('Europe/London'));
$d2 = new DateTime("2015-05-01 00:00:00.000000", new DateTimeZone('Europe/London'));

$diff = $d2->diff($d1);

print_r($diff);

      



Output

DateInterval Object ( [y] => 0 [m] => 1 [d] => 30 [h] => 0 [i] => 0 [s] => 0 [f] => 0 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 1 [days] => 61 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )

      

0


source







All Articles