0

PHP 5.5.1.14:

$d1 = new DateTime("2014-04-01 00:00");
$d2 = new DateTime("2014-07-01 00:00");
$d3 = $d2->diff($d1);
echo $d3->m . " months, " . $d3->d . " days";

returns

2 months, 30 days

Is there a way to reliably get the intuitive result 3 months? (Is this a bug or not?) BTW, when using May1st and Aug 1st, it gives the expected result of 3 months...

MBaas
  • 7,248
  • 6
  • 44
  • 61

1 Answers1

1

If time doesn't matter and you need just months diff, you can take second date with '23:59' time.

$d1 = new DateTime("2014-04-01 00:00");
$d2 = new DateTime("2014-07-01 23:59");
$d3 = $d2->diff($d1);
echo $d3->m . " months"

returns

3 months

And all other date pairs since 2014-01-01 - 2014-04-01 until 2014-12-01 - 2015-03-01 will return 3 months

aimar
  • 66
  • 4