2015-11-09 180 views
1

我想要计算一个php项目的两个日期之间的差异。如何计算两个日期之间的差异正确php

例如: 是什么六月的4月1日的第2次之间的区别?

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

计算差值
的第二种方法,我们计数可为1个月。然后添加4月2日至30日(28日)以及6月(1日)的日期=> 1个月和29天。

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

所以刚开始我试图用日期时间:: 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年2月3日”之间的差异,“2015年4月2日”应该是1个月和30天,(02-03至03-03 = 1个月。然后我们计算的天休息04-02,它们是30),BUT的差异计算出它作为1个月和27天(I估计是计算由我上述的第二方法中的差异)。因此它要么计算错误,要么第二种方法是正确的计算方法。

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

而且2015年5月1日和2015年2月28日之间的差异应该是2个月1天,但DIFF函数返回2个月3天

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

+1

https://github.com/fightbulc/moment.php –

回答

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"; 

} 
0

Php与DateInterval有bug。现在您可以尝试使用外部实现,如Moment库,或者关注错误状态并等待它修复。

-2

这里是一个小例子,希望它有助于

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