PHP计算两个时间之间的工作耗时
PHP计算两个时间之间的工作耗时,每天上班时间为上午9点到12点,下午13点半到18点半,其他时间工作时间不算。
public function calcTime($start_time, $end_time, $rest_day){
$dt_start = strtotime($start_time);
$dt_end = strtotime($end_time);
$timestamp = 0;
$dt_start_new = $dt_start;
while (strtotime(date('Y-m-d', $dt_start_new)) <= strtotime(date('Y-m-d', $dt_end))){
if(!in_array(date('Y-m-d', $dt_start_new), $rest_day)) {
$start_date_arr = getdate($dt_start);
$start_hours = $start_date_arr['hours'] + round($start_date_arr['minutes'] / 60, 2);
$end_date_arr = getdate($dt_end);
$end_hours = $end_date_arr['hours'] + round($end_date_arr['minutes'] / 60, 2);
$start_hours_9 = strtotime(date('Y-m-d 09:00:00', $dt_start));
$start_hours_12 = strtotime(date('Y-m-d 12:00:00', $dt_start));
$start_hours_13_5 = strtotime(date('Y-m-d 13:30:00', $dt_start));
$start_hours_18_5 = strtotime(date('Y-m-d 18:30:00', $dt_start));
$end_hours_9 = strtotime(date('Y-m-d 09:00:00', $dt_end));
$end_hours_12 = strtotime(date('Y-m-d 12:00:00', $dt_end));
$end_hours_13_5 = strtotime(date('Y-m-d 13:30:00', $dt_end));
$end_hours_18_5 = strtotime(date('Y-m-d 18:30:00', $dt_end));
if(date('Y-m-d', $dt_start) == date('Y-m-d', $dt_end)){
if($start_hours < 9){
if($end_hours < 9){
$timestamp += 0;
}else if($end_hours >= 9 && $end_hours <= 12){
$timestamp += $dt_end - $start_hours_9;
}else if($end_hours > 12 && $end_hours < 13.5){
$timestamp += $start_hours_12 - $start_hours_9;
}else if($end_hours >= 13.5 && $end_hours <= 18.5){
$timestamp += $start_hours_12 - $start_hours_9 + ($dt_end - $start_hours_13_5);
}else if($end_hours > 18.5){
$timestamp += $start_hours_12 - $start_hours_9 + ($start_hours_18_5 - $start_hours_13_5);
}
}else if($start_hours >= 9 && $start_hours <= 12){
if($end_hours <= 12){
$timestamp += $dt_end - $dt_start;
}else if($end_hours > 12 && $end_hours < 13.5){
$timestamp += $start_hours_12 - $dt_start;
}else if($end_hours >= 13.5 && $end_hours <= 18.5){
$timestamp += $start_hours_12 - $dt_start + ($dt_end - $start_hours_13_5);
}else if($end_hours > 18.5){
$timestamp += $start_hours_12 - $dt_start + ($start_hours_18_5 - $start_hours_13_5);
}
}else if($start_hours > 12 && $start_hours < 13.5){
if($end_hours > 12 && $end_hours < 13.5){
$timestamp += 0;
}else if($end_hours >= 13.5 && $end_hours <= 18.5){
$timestamp += $dt_end - $start_hours_13_5;
}else if($end_hours > 18.5){
$timestamp += $start_hours_18_5 - $start_hours_13_5;
}
}else if($start_hours >= 13.5 && $start_hours <= 18.5){
if($end_hours >= 13.5 && $end_hours <= 18.5){
$timestamp += $dt_end - $dt_start;
}else if($end_hours > 18.5){
$timestamp += $start_hours_18_5 - $dt_start;
}
}else if($start_hours > 18.5){
$timestamp += 0;
}
}else if(date('Y-m-d', $dt_start_new) != date('Y-m-d', $dt_start) && date('Y-m-d', $dt_start_new) != date('Y-m-d', $dt_end)){
$timestamp += 8 * 3600;
}else if(date('Y-m-d', $dt_start) == date('Y-m-d', $dt_start_new)){
if($start_hours < 9){
$timestamp += 8 * 3600;
}else if($start_hours >= 9 && $start_hours <= 12){
$timestamp += $start_hours_12 - $dt_start + ($start_hours_18_5 - $start_hours_13_5);
}else if($start_hours > 12 && $start_hours < 13.5){
$timestamp += $start_hours_18_5 - $start_hours_13_5;
}else if($start_hours >= 13.5 && $start_hours <= 18.5){
$timestamp += $start_hours_18_5 - $dt_start;
}else if($start_hours > 18.5){
$timestamp += 0;
}
}else if(date('Y-m-d', $dt_end) == date('Y-m-d', $dt_start_new)){
if($end_hours < 9){
$timestamp += 0;
}else if($end_hours >= 9 && $end_hours <= 12){
$timestamp += $dt_end - $end_hours_9;
}else if($end_hours > 12 && $end_hours < 13.5){
$timestamp += $end_hours_12 - $end_hours_9;
}else if($end_hours >= 13.5 && $end_hours <= 18.5){
$timestamp += $end_hours_12 - $end_hours_9 + ($dt_end - $end_hours_13_5);
}else if($end_hours > 18.5){
$timestamp += $end_hours_12 - $end_hours_9 + ($end_hours_18_5 - $end_hours_13_5);
}
}
}
$dt_start_new = strtotime('+1 day',$dt_start_new);
}
$hour = round(($timestamp)/3600, 2);
return $hour;
}
$start_time = '2020-12-30 16:36:20';
$end_time = '2021-01-04 09:30:30';
$rest_day = ['2021-01-01', '2021-01-02', '2021-01-03'];
$hours = $this->calcTime($start_time, $end_time, $rest_day);