计算PHP中两个日期之间的月数?

如果不使用PHP

5.3的date_diff函数(我使用的是PHP

5.2.17),是否有一种简单而准确的方法?我正在考虑以下代码,但是我不知道如何计算leap年:

$days = ceil(abs( strtotime('2000-01-25') - strtotime('2010-02-20') ) / 86400);

$months = ???;

我正在尝试计算一个人的月数。

回答:

$date1 = '2000-01-25';

$date2 = '2010-02-20';

$ts1 = strtotime($date1);

$ts2 = strtotime($date2);

$year1 = date('Y', $ts1);

$year2 = date('Y', $ts2);

$month1 = date('m', $ts1);

$month2 = date('m', $ts2);

$diff = (($year2 - $year1) * 12) + ($month2 - $month1);

您可能还希望将某天包括在内,具体取决于您是否指的是 整个 月。希望你能明白。

以上是 计算PHP中两个日期之间的月数? 的全部内容, 来源链接: utcz.com/qa/429055.html

回到顶部