php统计相差小于30天的月份数量

php count month quantity with difference less than 30 days

我需要独立于日期计算月份之间的差异。 如果我这样做:

$todaStart = new DateTime("2015-06-30");
$dateEnd = new DateTime("2015-07-01");

$diff = $todaStart->diff($dateEnd);
var_dump($diff);

object(DateInterval)[3]
public 'y' => int 0
public 'm' => int 0
public 'd' => int 1
public 'h' => int 0
public 'i' => int 0
public 's' => int 0
[...]

"m"参数是0,但是月份变了,所以对于我需要做的计数,我需要结果是1

如果我将月份更改为“08”,也会发生同样的情况,它应该计算 2 个月的差异,但响应是 1 个月。

在网上挖掘我在 this 网站上找到了这个解决方案,希望它能帮助其他人。

 $d1= new DateTime("2015-06-01");
 $d2= new DateTime("2015-07-30");

 $y1 = $d1->format('Y');
 $m1 = $d1->format('m');
 $y2 = $d2->format('Y');
 $m2 = $d2->format('m');

 $diff = (($y2 - $y1) * 12) + ($m2 - $m1);
 // var_dump($diff) output 1