PHP倒计时时间字符串格式化程序函数


PHP Countdown time string formatter function

我创建了一个php函数,它格式化经过的时间,并将正常的时间戳作为输入参数。不存在物理问题,而且它按预期工作,我的问题是,有没有办法简化它?:

PHP

function time_elapsed_string($difference)
{       
    //Days
    $days = round(($difference / 86400), 2);
    //Hours
    $hours = floor($difference / 3600);
    if($hours >= 24) {
        $remainderHours = fmod($hours, 24); // Get the remainder from the days.
        if ($remainderHours < 10) {
            $remainderHours = '0' . $remainderHours;
        }
    } else {
        $remainderHours = $hours; 
        $days = 0;

        if ($remainderHours < 10) {
            $remainderHours = '0' . $hours;
        }
    }   
    //Minutes
    $mins = floor($difference / 60);
    if ($mins >= 60){
        $remainderMins = fmod($mins, 60);
        if ($remainderMins < 10) {
            $remainderMins = '0' . $remainderMins;
        }
    } else {
        $remainderMins = $mins;
        if ($remainderMins < 10) {
            $remainderMins = '0' . $remainderMins;
        }
    }
    //Seconds
    $seconds = floor($difference);
    if($seconds >= 60) {
        $remainderSeconds = fmod($seconds, 60); 
        if ($remainderSeconds < 10) {
            $remainderSeconds = '0' . $remainderSeconds;
        }
    } else {
        $remainderSeconds = $seconds;
        if ($remainderSeconds < 10) {
            $remainderSeconds = '0' . $remainderSeconds;
        }
    }
    //Format day due to days being reset to 0 format in hours fuction
    $days = (floor($days) < 10 ? ('0' . floor($days)) : floor($days));
    return  $days . ':' . $remainderHours . ':' . $remainderMins     . ':' . $remainderSeconds;
}

是的,您很可能使用DateInterval对象以更清晰的方式来实现:http://php.net/manual/en/class.dateinterval.php