如何计算一天工作的小时数?
Employee should work 8 hours so 8 x 3600 = 28,800 second lets say the employee only works for 4 hours 4 x 3600 = 14,400 so the result would be 14,400 - 28,800 = -14,400 so how do i convert the result to hours and minutes and seconds
RESULT = - 04:00:00
also lets say 8 hours should work 8 x 3600 = 28,000 seconds lets say the employee works 12 hours 12 x 3600 = 43,200 = 43,200 seconds result would be 43,200 - 28,000 = 15,200
RESULT = + 04:00:00
<?php
$hoursworked = 4*3600
$dd = 8*3600;
echo gmdate("H:i:s", $dd%86400);
// day total
$ee =$hoursworked - $dd ;
$seconds = $ee;
$hours = floor($seconds / 3600);
$mins = floor(($seconds - $hours*3600) / 60);
$s = $seconds - ($hours*3600 + $mins*60);
$mins = ($mins<10?"0".$mins:"".$mins);
$s = ($s<10?"0".$s:"".$s);
$timea = ($hours>0?$hours.":":"").$mins.":".$s; ?>
我想从上面的例子得到的结果是-04:00:00因为员工只工作了4个小时而应该工作8谢谢
答案 0 :(得分:0)
尝试以下代码。
function calc($work_hr, $tot_hr)
{
$tot = $tot_hr * 3600;
$work = $work_hr * 3600;
if ($tot > $work) {
$seconds = $tot - $work;
$total = "- " . gmdate("H:i:s", $seconds);
} else {
$seconds = $work - $tot;
$total = "+ " . gmdate("H:i:s", $seconds);
}
return $total;
}
echo "Less Hours :" . calc(4,8); //Output : - 04:00:00
echo "<br>";
echo "Extra Hours:" . calc(12,8); //Output : + 04:00:00
echo "<br>";
echo "Extra Hours:" . calc(9.5,8); //Output : + 01:30:00
答案 1 :(得分:0)
尝试此功能
<?php
function formatTime($hours) {
$seconds = $hours * 3600;
$worked = $hours - 8;
$formatedhours = sprintf("%02d", abs($worked)) . ':00:00';
// WHEN WORKED FOR LESS THAN ONE HOUR
if($hours < 8) {
$formatedhours = '-' . $formatedhours;
}
return $formatedhours;
}
$hours = 8; //hours
echo formatTime($hours);