2011-07-28 77 views

回答

3

這就像

//function to convert seconds into hour:minute:second 
    function sec2hms ($sec, $padHours = false) 
    { 

     // start with a blank string 
     $hms = ""; 

     // do the hours first: there are 3600 seconds in an hour, so if we divide 
     // the total number of seconds by 3600 and throw away the remainder, we're 
     // left with the number of hours in those seconds 
     $hours = intval(intval($sec)/3600); 

     // add hours to $hms (with a leading 0 if asked for) 
     $hms .= ($padHours) 
     ? str_pad($hours, 2, "0", STR_PAD_LEFT). ":" 
     : $hours. ":"; 

     // dividing the total seconds by 60 will give us the number of minutes 
     // in total, but we're interested in *minutes past the hour* and to get 
     // this, we have to divide by 60 again and then use the remainder 
     $minutes = intval(($sec/60) % 60); 

     // add minutes to $hms (with a leading 0 if needed) 
     $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ":"; 

     // seconds past the minute are found by dividing the total number of seconds 
     // by 60 and using the remainder 
     $seconds = intval($sec % 60); 

     // add seconds to $hms (with a leading 0 if needed) 
     $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT); 

     // done! 
     return $hms; 

    } 

    $subtracted_time = strtotime($time2) - strtotime($time1); //gives difference in seconds 
    echo(sec2hms($subtracted_time)); 

功能sec2hms源http://www.laughing-buddha.net/php/lib/sec2hms/

+0

使用此代碼,使用問題中提供的值,'$ subtracted_time'將得到'10:12:45'作爲結果,這不對應於時間差!如果我們只運行第一行,我們將在幾秒鐘內獲得正確的值。 –

+0

是的,你是對的,我已經更新了答案 – Ehtesham

+0

在'日期'功能'm'是MONTH。我應該使用幾分鐘。無論如何,'date'不應該用在這種情況下。它認爲你的電腦時間區域設置 - 結果將根據系統而有所不同。 –

2

我會使用strtotime()函數將時間轉換爲UNIX時間值。然後,您可以減去這兩個值,因爲它們將是整數,然後使用date()函數以您想要的方式格式化差異。

+1

'date()'functio n需要一個時間戳參數,即「自Unix紀元(1970年1月1日00:00:00 GMT)以來的秒數中計算的時間」,並且不用於此。它考慮時間區域設置。在我的機器上,'echo date('H:i:s',0);'輸出'21:00:00'因爲我的語言環境是格林威治標準時間-3。 –

+0

啊,我沒有想到這一點。謝謝! – BenGC

0

作爲除了BenGC的回答

$tmp_time1 = strtotime($submit_date); 
$tmp_time2 = strtotime($submit_date); 
$timelength = $tmp_time2 - $tmp_time1; 

應該工作,我沒有測試過,但我已經使用代碼非常相似。你會得到兩次之間的秒數。

2

這是我的建議(工作PHP代碼):

$time1 = '12:45:03'; 
$time2 = '14:03:48'; 
$timelength = strtotime($time2) - strtotime($time1); 

$hours = intval($timelength/3600); 
$minutes = intval(($timelength % 3600)/60); 
$seconds = $timelength % 60; 

echo str_pad($hours, 2, '0', STR_PAD_LEFT) . ':' . str_pad($minutes, 2, '0', STR_PAD_LEFT) . ':' . str_pad($seconds, 2, '0', STR_PAD_LEFT); 

輸出:

1時18分45秒

0

使用以下,這將有助於你

function calculate_time_past($start_time, $end_time, $format = "s") { 
     $time_span = strtotime($end_time) - strtotime($start_time); 
     if ($format == "s") 
     { // is default format so dynamically calculate date format 
      if ($time_span > 60) { $format = "i:s"; } 
      if ($time_span > 3600) { $format = "H:i:s"; } 
     } 
     return gmdate($format, $time_span); 
} 

$tdiff=calculate_time_past($time1, $time2, "H:i:s"); // will output 00:02:45 when format is overridden