我试图确定两个日期之间的日期间隔数。间隔可以是几个月,几天,几小时或用户选择的任何数量。如何查找PHP中两个日期之间的DateIntervals数
此刻我有一个使用递归的函数,但是,如果由于递归嵌套限制而导致两点之间存在超过100个间隔,则会中断。
private static function findCurrentInterval($initialStartTimestamp, $intervalStrtotimeFormat, $now = null, $period = 1)
{
if (is_null($now)){
$now = time();
}
$endOfCurrentInterval = strtotime($intervalStrtotimeFormat, $initialStartTimestamp);
if ($now > $initialStartTimestamp && $now < $endOfCurrentInterval){
return new self($period);
}else{
# increment the period ID tracking variable
$period++;
return self::findCurrentInterval($endOfCurrentInterval, $intervalStrtotimeFormat, $now, $period);
}
}
我需要出来的是代表我们对哪个时间间隔的周期在当前,也就是说,如果它被设置为7天间隔的整数值,20天已开始点之间传递现在,它会返回'3',因为我们会进入第三阶段。
有没有办法做到这一点没有递归,也许使用DateInterval?
N.B.如果将其限制在几天或几小时内,这将非常简单 - 但它需要支持几个月,这可能会有不同的长度。