2013-05-08 69 views
2

我嘗試比較2日期時間,並得到在幾分鐘內不同和第二後,我是指這個話題How to get time difference in minutes in PHP是該代碼可以顯示不同,但在分:計算時間分不同和第二

$to_time = strtotime("2008-12-13 18:42:00"); 
$from_time = strtotime("2008-12-13 18:41:58"); 

echo round(abs($to_time - $from_time)/60,2). " minute"; 

所以如何顯示在分鐘和秒從上面的代碼?我的PHP版本是5.2.17

回答

3
$minutes = round(abs($to_time - $from_time)/60,2); 
$seconds = abs($to_time - $from_time) % 60; 

echo "$minutes minute, $seconds seconds"; 
+0

由於其現在的工作。 – rusly 2013-05-08 03:07:20

2

替代地使用用於DateTime class PHP> = 5.3: -

$to_time = new \DateTime('2008-12-13 18:42:00'); 
$from_time = new \DateTime('2008-12-13 18:41:58'); 
$diff = $from_time->diff($to_time); 
echo $diff->format('%i Minutes %s Seconds'); 

注:'$差異」將是DateInterval一個實例。

或者稍微更簡潔,但不易閱讀: -

$to_time = new \DateTime('2008-12-13 18:42:00'); 
$from_time = new \DateTime('2008-12-13 18:41:58'); 

echo $to_time->diff($from_time)->format('%i Minutes %s Seconds');