DateInterval返回0个月?

I want to calculate how many months there are between two dates:

$from = new DateTime('2014-11-01 00:00:00');
$to = new DateTime('2014-12-01 00:00:00');
$diff = $from->diff($to);
echo $diff->m; // <-- 0

The difference between 2014-11-01 00:00:00 and 2014-12-01 00:00:00 should be exactly one month, right? But $diff->m says 0. diff->d says 30 days instead.

If I try the same with 2014-01-01 00:00:00 and 2014-02-01 00:00:00, $diff->m says 1 month now, which is correct.

Did I miss something?


EDIT:

As I only need to calculate how many months there are between the two set dates and the user inserts the from and to dates with month and year (12/2014), the best solution working for me is simply increasing the to date:

// user inserts
// from: 11/2014
// to: 12/2014

$from = new DateTime('2014-11-01');
$to = new DateTime('2014-12-15');
$diff = $from->diff($to);

echo $diff->m // outputs 1

Thanks STLMikey for feeding me the answer! ;)

DateInterval works a bit...oddly in many scenarios.

Try $diff->format('%m month, %d days'); and see if that helps clarify some things. %m and %d often return 0 if the difference is better covered by a different measure.

for example:

$from = new \DateTime('2014-01-01 00:00:00'); $to = new \DateTime('2014-02-15 00:00:00');

$diff->m of these two dates will also return "1", as per the documentation.

I personally tend to just use $interval->format('%a total days'); as it doesn't confuse itself (or me) trying to be too smart.

On my computer the code produces different results. My php version is 5.3.13.

<?php

$from = new DateTime('2014-11-01 00:00:00');
$to = new DateTime('2014-12-01 00:00:00');
$diff = $from->diff($to);
echo $diff->m; // outputs 1
echo $diff->d; // outputs 0

?>

EDIT 1: another output

<?php

$from = new DateTime('2014-01-01 00:00:00');
$to = new DateTime('2014-02-01 00:00:00');
$diff = $from->diff($to);
echo $diff->m; // outputs 1
echo $diff->d; // outputs 0

?>