如何正确计算2个日期之间的差异php

时间:2015-11-09 12:21:13

标签: php date datetime date-difference

我正在尝试计算php项目的2个日期之间的差异。

例如: 4月2日到6月1日有什么区别?

计算差异的第一种方法
从4月2日到5月2日= 1个月。从5月2日到6月1日= 30天。 => 4月2日至6月1日= 1个月30天

计算差异的第二种方法
我们算一个月。然后添加4月2日至30日(28天)的日期和6月(1天)的日期=> 1个月29天。

我认为第一种方法是正确的,只是因为我认为大多数人都是这样计算的。

所以一开始我尝试使用DateTime :: diff()函数

function dateDiff($date1, $date2 = false) {
if (!$date2)
    $date2 = date('Y-m-d');

$datetime1 = new DateTime($date1 , new DateTimeZone('EUROPE/Sofia'));
$datetime2 = new DateTime($date2 , new DateTimeZone('EUROPE/Sofia'));

$interval = $datetime1->diff($datetime2);

$y = $interval->format('%y');
$m = $interval->format('%m');
$d = $interval->format('%d');

return $y . " " . $m . " " . $d;

}

但我注意到它没有正确计算差异。 “2015-02-03”和“2015-04-02”之间的差异应为 1个月30天,(02-03至03-03 = 1个月。然后我们计算其余的到04-02的日子,这是30),差异计算为 1个月和27天(我猜它是通过第二种方法计算差异我在上面说过)。因此,计算错误或第二种方法是正确的计算方法。

BUT 让我们看看这个例子: 2015-05-01和2015-03-31(这次我们要倒退了)。差异返回 1个月差异,我认为它应该 1个月和1天

此外,2015-05-01和2015-02-28之间的差异应为 2个月和1天,但diff函数返回 2个月和3天

那么,这是计算2个日期之间差异的正确方法吗? DateTime :: diff()是否正确计算?有没有办法通过第一种方法计算两个日期之间的差异。

3 个答案:

答案 0 :(得分:0)

Php有DateInterval的错误。现在,您可以尝试使用Moment库之类的外部实现,或者跟踪错误状态并等待它何时修复。

答案 1 :(得分:0)

这应该正确计算差异。

function monthDiff($m1, $m2) {
    if($m1 > $m2) {
        return 12 - $m1 + $m2;
    }
    return $m2 - $m1;
}

function yearDiff($y1, $y2) {
    return $y2 - $y1;
}


function checkLeapYear($year){
    $year = (int)$year;
    return ( ( ($year % 4 == 0 && ($year % 100) != 0 ) || ( ($year % 100) == 0 && ($year % 400) == 0 ) ) ? 1 : 0);
}


function dateDiff($date1, $date2 = false) {
    if (!$date2)
        $date2 = date('Y-m-d');

    $datetime1 = new DateTime($date1 , new DateTimeZone('EUROPE/Sofia'));
    $datetime2 = new DateTime($date2 , new DateTimeZone('EUROPE/Sofia'));

    if($datetime1 > $datetime2){ //always go from smaller to bigger date
        $temp = $datetime1;
        $datetime1 = $datetime2;
        $datetime2 = $temp; 
    }

    $d1 = (int)$datetime1->format('d');
    $d2 = (int)$datetime2->format('d');

    $m1 = (int)$datetime1->format('m');
    $m2 = (int)$datetime2->format('m');

    $y1 = (int)$datetime1->format('Y');
    $y2 = (int)$datetime2->format('Y');


    $leapYear = checkLeapYear($y1); 
    $daysInMonth1 = [1 => 31, 28 + $leapYear, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; // the number of days in the months

    $leapYear = checkLeapYear($y2);
    $daysInMonth2 = [1 => 31, 28 + $leapYear, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];


    $monthCorrection = 0;


    if ($d1 < $d2) {
        $d = $d2 - $d1; 
    }

    if ($d1 > $d2){
        if ($daysInMonth2[$m2] >= $d1){ 
            $d = $daysInMonth1[$m1] - $d1 + $d2;;
        }
        else {
            $d = $daysInMonth1[$m1] - $d1 + $d2;
        }
        $monthCorrection = -1;
    }

    if ($d1 == $d2 ){
        $d = 0;
    }

    $m = monthDiff($m1, $m2) + $monthCorrection;

    $y = yearDiff($y1, $y2);
    if ($m1 > $m2){
        $y--;
    }

    return $y . " years " . $m . " months " . $d . " days";

}

答案 2 :(得分:-2)

这是一个小例子,希望它有所帮助

$date1 = strtotime("2015-01-01"); //yyyy-mm-dd
$date2 = strtotime("2015-01-08");
$datediff = $date2 - $date1;
echo floor($datediff/(60*60*24));