date_diff returns incorrect difference

0

I was doing tests with this function, but it returns an incorrect difference, how do I get around this?

Example: when compared the difference between 01/01 and 01/03 returns the difference of one month, and only when it is 03/03 to return the two months.

Correct:

$datetime1 = new DateTime('2009-01-01');
$datetime2 = new DateTime('2009-03-04');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
echo $interval->format('%R%m month');

Result:

  

+62 days + 2 month

Incorrect:

$datetime1 = new DateTime('2009-01-01');
$datetime2 = new DateTime('2009-03-01');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
echo $interval->format('%R%m month');

Result:

  

+59 days + 1 month

    
asked by anonymous 26.10.2018 / 19:28

1 answer

0

The DateInterval :: format () method does not recalculate transport points in time sequences or in date segments. This is expected because it is not possible to burst values such as "32 days", which could be interpreted as something from "1 month and 4 days" to "1 month and 1 day". PHP Manual

    
26.10.2018 / 20:49