2016-05-12 20 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; 
} 

答えて

関連する問題