2012-12-12 137 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