3 回答
TA贡献1828条经验 获得超13个赞
function hoursandmins($time, $format = '%02d:%02d'){
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}
$start_time_name = "05:00";
$end_time_name = "10:00";
$total_break_name = "30"; //in minutes
$start_time_name = strtotime($start_time_name);
$end_time_name = strtotime($end_time_name);
$total_break_name = (int) $total_break_name * 60; //convert into seconds
$total_hours_work_cal = $end_time_name - $total_break_name - $start_time_name; //total time will be in seconds
$total_hours_work_cal = $total_hours_work_cal / 60; //converted into minutes;
$total_hours_work_cal = hoursandmins($total_hours_work_cal); //convert into hours and minutes
echo $total_hours_work_cal;
TA贡献1799条经验 获得超6个赞
/* justfor testing purpose to have input */
$_POST['start_time_name'] = "05:00";
$_POST['total_break_name'] = "00:30";
$_POST['end_time_name'] = "10:00";
$start_time_name = $_POST['start_time_name'];
$end_time_name = $_POST['end_time_name'];
$total_break_name = $_POST['total_break_name'];
$end_time = mktime(explode(":",$end_time_name)[0],explode(":",$end_time_name)[1]);
$start_time = mktime(explode(":",$start_time_name)[0],explode(":",$start_time_name)[1]);
$total_break = mktime(explode(":",$total_break_name)[0],explode(":",$total_break_name)[1]);
$diff = $end_time + $total_break - $start_time;
$total_hours_work_cal = date("H:i",$diff);
echo "TOTAL HOURS OF WORK : " . $total_hours_work_cal;
// TOTAL HOURS OF WORK : 05:30
TA贡献1872条经验 获得超3个赞
<?php
/*
$total_hours_work_cal = ($end_time_name - $total_break_name);
The calculation for this would be;
End time = 10:00
Start Time = 05:00
Break Time = 00:30
*/
$start = new \DateTime('05:00:00');
$end = new \DateTime('10:00:00');
$break = new \DateInterval('PT30M');
$beginEndInterval = $end->diff($start);
$total = new \DateTime('00:00');
$totalComp = new \DateTime('00:00');
$total->add($beginEndInterval);
$total->add($break);
echo $totalComp->diff($total)->format('%H:%I'); // 04:30
- 3 回答
- 0 关注
- 131 浏览
添加回答
举报