首页 > 代码库 > php计算时间差/两个时间日期相隔的天数,时,分,秒.

php计算时间差/两个时间日期相隔的天数,时,分,秒.

<?php
function timediff( $begin_time, $end_time )
{
    if ( $begin_time < $end_time ) {
        $starttime = $begin_time;
        $endtime = $end_time;
    } else {
        $starttime = $end_time;
        $endtime = $begin_time;
    }
    $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( "day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs );
    return $res;
}
//======== 实例使用 ========
$timediff = timediff( strtotime( "2011-10-28" ), strtotime( "2011-10-29" ) );
print_r( $timediff );
?>


这个直接返回数组 很实用。。

 

一下还有:

来自:http://www.zh30.com/php-time-difference-between-two-dates.html

我修改了下:

<?php
$one = strtotime(‘2011-12-08 07:02:40‘);//开始时间 时间戳
$tow = strtotime(‘2011-12-25 00:00:00‘);//结束时间 时间戳
$cle = $tow - $one; //得出时间戳差值

/* 这个只是提示
echo floor($cle/60); //得出一共多少分钟
echo floor($cle/3600); //得出一共多少小时
echo floor($cle/3600/24); //得出一共多少天
*/
/*Rming()函数,即舍去法取整*/
$d = floor($cle/3600/24);
$h = floor(($cle%(3600*24))/3600);  //%取余
$m = floor(($cle%(3600*24))%3600/60);
$s = floor(($cle%(3600*24))%60);

echo "两个时间相差 $d$h 小时 $m$s 秒"
?>