2014-07-10 92 views
1

我需要將時間28:10:10(在HH:MM:SS中)轉換爲1:04:10:10 (日:HH:MM:SS)。我想使用apply strtime()函數將輸入時間轉換爲24小時時間格式。如何將HH:MM:SS時間轉換爲天數:HH:MM:SS in php

此外,我需要從它減去給定的時間。

+0

有你使用DateTime對象這個考慮? – gries

+0

關於PHP日期/時間函數的文章:** [使用PHP處理日期時間](http://www.devshed.com/c/a/PHP/DateTime-Processing-with-PHP/)** –

回答

2
**I have tried to answer your question. below is the code that will give you correct output. pls have a look on the below code.** 

function secondsToTime($ss) { 
     $s = $ss%60; 
     $m = floor(($ss%3600)/60); 
     $h = floor(($ss%86400)/3600); 
     $d = floor(($ss%2592000)/86400); 

     // Ensure all values are 2 digits, prepending zero if necessary. 
     $s = $s < 10 ? '0' . $s : $s; 
     $m = $m < 10 ? '0' . $m : $m; 
     $h = $h < 10 ? '0' . $h : $h; 
     $d = $d < 10 ? '0' . $d : $d; 

     return "$d:$h:$m:$s"; 
    } 



    $time = "28:10:10"; 
    $timeArr = array_reverse(explode(":", $time)); 
    $seconds = 0; 
    foreach ($timeArr as $key => $value) 
    { 
     if ($key > 2) break; 
     $seconds += pow(60, $key) * $value; 
    } 
    $seconds; 

    print secondsToTime($seconds); 

OUTPUT 1:04:10:10

+0

爲什麼要打擾幾小時/分鐘/秒?他們不會改變。 –

+0

爲什麼他們不會改變?如問題HH:MM:SS改變。 –

相關問題