我想知道如果它是在PHP /笨能夠減去其在的在PHP中減去時間(HH:MM:SS)的一串數字的方法?
HH一個時間格式的值:MM:SS
例如:
$time1 = "12:45:03";
$time2 = "14:03:48";
$timelength = $time2- $time1;
任何建議或鏈接到代碼示例?
我想知道如果它是在PHP /笨能夠減去其在的在PHP中減去時間(HH:MM:SS)的一串數字的方法?
HH一個時間格式的值:MM:SS
例如:
$time1 = "12:45:03";
$time2 = "14:03:48";
$timelength = $time2- $time1;
任何建議或鏈接到代碼示例?
這就像
//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));
我會使用strtotime()
函數將時間轉換爲UNIX時間值。然後,您可以減去這兩個值,因爲它們將是整數,然後使用date()
函數以您想要的方式格式化差異。
'date()'functio n需要一個時間戳參數,即「自Unix紀元(1970年1月1日00:00:00 GMT)以來的秒數中計算的時間」,並且不用於此。它考慮時間區域設置。在我的機器上,'echo date('H:i:s',0);'輸出'21:00:00'因爲我的語言環境是格林威治標準時間-3。 –
啊,我沒有想到這一點。謝謝! – BenGC
作爲除了BenGC的回答
$tmp_time1 = strtotime($submit_date);
$tmp_time2 = strtotime($submit_date);
$timelength = $tmp_time2 - $tmp_time1;
應該工作,我沒有測試過,但我已經使用代碼非常相似。你會得到兩次之間的秒數。
這是我的建議(工作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秒
使用以下,這將有助於你
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
使用此代碼,使用問題中提供的值,'$ subtracted_time'將得到'10:12:45'作爲結果,這不對應於時間差!如果我們只運行第一行,我們將在幾秒鐘內獲得正確的值。 –
是的,你是對的,我已經更新了答案 – Ehtesham
在'日期'功能'm'是MONTH。我應該使用幾分鐘。無論如何,'date'不應該用在這種情況下。它認爲你的電腦時間區域設置 - 結果將根據系統而有所不同。 –