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";

返回

2个月30天

有没有办法在 3 个月内可靠地获得直观的结果?(这是不是一个错误?)顺便说一句,当使用 May1st 和 Aug 1st 时,它给出了 3 个月的预期结果......

4

1 回答 1

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

于 2014-07-30T09:46:58.667 回答