首页 > 代码库 > 获取两个日期的时间差(天数、时、分、秒)

获取两个日期的时间差(天数、时、分、秒)

其实这个时间在MySQL中处理,但是本着尽量避免逻辑在数据库中处理的原则,还是在程序断处理更佳,减少数据库的压力,大负载也是这种思想。
获取两个日期的时间差(天数、时、分、秒)
function timeDiff( $beginTime, $endTime ) 
{ 
    if ( $beginTime < $endTime ) { 
        $starttime = $beginTime; 
        $endtime = $endTime; 
    } else { 
        $starttime = $endTime; 
        $endtime = $beginTime; 
    } 
    $timediff = $endtime - $starttime; 
    $days = intval( $timediff / 86400 ); 
    $remain = $timediff % 86400; 
    $hours = intval( $remain / 3600 ); 
    $remain = $remain % 3600; 
    $mins = intval( $remain / 60 ); 
    $secs = $remain % 60; 
    $res = array( "dayNum" => $days, "hourNum" => $hours, "minNum" => $mins, "secNum" => $secs ); 
    return $res; 
} 

$timediff = timeDiff( strtotime( "2014-08-30 00:00:00" ), strtotime( "2014-09-11 18:18:41" ) ); 
print_r( $timediff ); 

获取两个日期的时间差(天数、时、分、秒)