“天between"超过一天边界时应返回1,甚至在24小时内


"Days between" should return 1 when going over a day boundary, even within 24 hours

我有当前功能:

function getDaysBetween($start, $end) {
    $start = strtotime($start);
    $end = strtotime($end);
    $dateDiff = abs($end- $start);
    $daysBetween = floor($dateDiff/(60*60*24));
    return $daysBetween;
}

上面的函数确实返回间隔的天数。

例如,8月3日凌晨3点减去8月2日晚上11点得到0。

在这种情况下,我希望它返回1,因为这是不同的日子。我怎样才能做到呢?

代替

$start = strtotime($start);
$end = strtotime($end);
使用

$start = strtotime('00:00:00', strtotime($start));
$end = strtotime('00:00:00', strtotime($end));

$start$end中删除时间,简单地传递日期。例如03/08/2011代替03/08/2011:03:00:00

function getDaysBetween($start, $end) {
    $start = new DateTime($start);
    $end = new DateTime($end);
    $start->setTime(0,0,0);
    $end->setTime(0,0,0);
    $dateDiff = abs($end->getTimestamp() - $start->getTimestamp());
    $daysBetween = floor($dateDiff/(60*60*24));
    return $daysBetween;
}