2016-05-12 90 views
0

我有一個PHP函數,我已經創建它格式化時間已過去,它需要一個正常的時間戳作爲輸入參數。有沒有生理問題和它的作品如預期,我的問題是,有沒有辦法簡化它?: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; 
} 

回答