我想得到具体的小时数之间的差异,因为我正在工作的工资项目,这需要获得一个员工的总工作时间。
假设雇员工作了40:18:20 (hh:mm:ss),他错过了12:15:10的工作(hh:mm:ss)
我想得到这两次的区别如下:(40:18:20) - (12:15:10) = (28:03:10)
它有可能通过PHP函数吗?
我实际上所做的,是把它分割成字符串,然后试着把每个数字分别减去,然后再回忆它们,这是“我认为的那样”是不专业的。
请给我建议。
发布于 2016-03-09 09:05:37
这将处理超过24小时的工作时间。
$start = date_create(gmdate('D, d M Y H:i:s',timeTosec('40:18:20')));
$end = date_create(gmdate('D, d M Y H:i:s',timeTosec('12:15:10')));
$diff=date_diff($end,$start);
print_r($diff);
function timeTosec($time){
sscanf($time, "%d:%d:%d", $hours, $minutes, $seconds);
$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;
return $time_seconds;
}发布于 2016-03-09 08:37:44
您可以使用此函数
function getTimeDiff($dtime,$atime){
$nextDay=$dtime>$atime?1:0;
$dep=explode(':',$dtime);
$arr=explode(':',$atime);
$diff=abs(mktime($dep[0],$dep[1],0,date('n'),date('j'),date('y'))-mktime($arr[0],$arr[1],0,date('n'),date('j')+$nextDay,date('y')));
//Hour
$hours=floor($diff/(60*60));
//Minute
$mins=floor(($diff-($hours*60*60))/(60));
//Second
$secs=floor(($diff-(($hours*60*60)+($mins*60))));
if(strlen($hours)<2)
{
$hours="0".$hours;
}
if(strlen($mins)<2)
{
$mins="0".$mins;
}
if(strlen($secs)<2)
{
$secs="0".$secs;
}
return $hours.':'.$mins.':'.$secs;}
发布于 2016-03-09 09:02:51
这将处理大于24小时的差异。代码可能有点被分解了,但另一方面,它很容易理解。
// Use any valid date in both cases
$a = new DateTime(date("Y-m-d H:i:s", mktime(5, 20, 15, 12, 31, 2016)));
$b = new DateTime(date("Y-m-d H:i:s", mktime(65, 10, 5, 12, 31, 2016)));
$c = $a->diff($b);
$days = $c->format("%a");
$hours = intVal($c->format("%H")) + intVal($days);
$minutes = $c->format("%m");
$seconds = $c->format("%s");
// Unformatted result
print $hours . ':' . $minutes . ':' . $seconds;https://stackoverflow.com/questions/35886518
复制相似问题