比较两个日期的时间差精确到秒

时间:2022-06-23 11:31:06
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;
}
$time = '2015-03-20,16:20:30';
$year=substr($time,0,4);//取得年份
$month=substr($time,5,2);//取得月份
$day=substr($time,8,2);//取得几号
$h = substr($time,11,2);//取得小时
$i = substr($time,14,2);//取得小时
$s = substr($time,17,2);//取得小时

$time3 = '2015-05-25,11:10:10';
$year1=substr($time3,0,4);//取得年份
$month1=substr($time3,5,2);//取得月份
$day1=substr($time3,8,2);//取得几号
$h1 = substr($time3,11,2);//取得小时
$i1 = substr($time3,14,2);//取得小时
$s1 = substr($time3,17,2);//取得小时
$shijian = timediff(strtotime('2015-03-20, 16:20:30'),strtotime('2015-05-25, 11:10:10'));
echo '从'.$year.'年'.$month.'月'.$day.'日'.$h.'时'.$i.'分'.$s.'秒到'.$year1.'年'.$month1.'月'.$day1.'日'.$h1.'时'.$i1.'分'.$s1.'秒经过了',$shijian['day'],'天'.$shijian['hour'].'时'.$shijian['min'].'分'.$shijian['sec'].'秒';