2012-12-12 135 views
-1

如何編寫計算總工作時間的PHP代碼?PHP計算總時間

例如:$starttime = 10:20
的工作時間結束::

的工作時間開始」 $stoptime = 12:59

所以總工作時間:$totaltime應該是:02:39

+1

你試過了什麼? – sicKo

+1

這聽起來像一個很好的工作:http://php.net/manual/en/class.datetime.php – zackg

+1

DateTime(如zackg已經提到)和DateInterval(http://www.php.net/manual/en /class.dateinterval.php)在這裏是你的朋友 –

回答

3

試試看這個代碼

$starttime = '10:20'; 
    $stoptime = '12:59'; 
    $diff = (strtotime($stoptime) - strtotime($starttime)); 
    $total = $diff/60; 
    echo sprintf("%02dh %02dm", floor($total/60), $total%60); 
5

使用本

<?php 
$start = strtotime("12/12/2012 10:20:00"); 

$end = strtotime("12/12/2012 12:59:00"); 

$totaltime = ($end - $start) ; 

$hours = intval($totaltime/3600); 
$seconds_remain = ($totaltime - ($hours * 3600)); 

$minutes = intval($seconds_remain/60); 
$seconds = ($seconds_remain - ($minutes * 60)); 

echo "$hours:$minutes:$seconds"; 
?> 
+0

請提供我的解決方案,如果我想從這個時間減去30分鐘,那麼該怎麼辦@ user7282 – Ghugu