格式化时间的函数

//格式化时间
function time_format($time, $start='d', $keep='s', $unit=true){
    
    $day = $hours = $minutes = $seconds = '';
    if($start == 'd'){
        //$time / 3600 得到小时数,再除以24小时,余数则为不足一天的小时数
        $day = floor($time / 3600 / 24);
        $hours = floor($time / 3600) % 24;
        $minutes = floor($time / 60) % 60;
        $seconds = $time % 60;
    }elseif($start == 'h'){
        $hours = floor($time / 3600);
        $minutes = floor($time / 60) % 60;
        $seconds = $time % 60;
    }elseif($start == 'i'){
        $minutes = floor($time / 60);
        $seconds = $time % 60;
    }
    
    //如果要带单位
    if($unit){
        $day = ($day)? $day.'天' : '';
        $hours = ($hours)? $hours.'小时' : '';
        $minutes = ($minutes)? $minutes.'分钟' : '';
        $seconds = ($seconds)? $seconds.'秒' : '';
    }
    
    $new['d'] = $day;
    $new['h'] = $hours;
    $new['i'] = $minutes;
    $new['s'] = $seconds;
    
    $time = '';
    foreach($new as $key => $val){
        if($key == $start) $go = true;
        if($go){
            if($unit){
                $time .= $val;
            }else{
                $time[$key]= $val;
            }
        }
        if($key == $keep) break;
    }
    return $time;
}
原文地址:https://www.cnblogs.com/tudou1223/p/4935205.html