PHP计算两个时间之间的工作耗时

PHP计算两个时间之间的工作耗时

PHP计算两个时间之间的工作耗时,每天上班时间为上午9点到12点,下午13点半到18点半,其他时间工作时间不算。

/**
	 * @param $start_time 开始时间
	 * @param $end_time 结束时间
	 * @param $rest_day 节假日
	 */
	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){ //开始时间小于9点
						if($end_hours < 9){ //结束时间小于9点
							$timestamp += 0;
						}else if($end_hours >= 9 && $end_hours <= 12){ //结束时间大于等于9点小于等于12点
							$timestamp += $dt_end - $start_hours_9;
						}else if($end_hours > 12 && $end_hours < 13.5){ //结束时间在12点-13点30之间
							$timestamp += $start_hours_12 - $start_hours_9;
						}else if($end_hours >= 13.5 && $end_hours <= 18.5){ //结束时间在13点30-18点30之间
							$timestamp += $start_hours_12 - $start_hours_9 + ($dt_end - $start_hours_13_5);
						}else if($end_hours > 18.5){ //结束时间在18点30之后
							$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){ //结束时间大于等于9点小于等于12点
							$timestamp += $dt_end - $dt_start;
						}else if($end_hours > 12 && $end_hours < 13.5){ //结束时间在12点-13点30之间
							$timestamp += $start_hours_12 - $dt_start;
						}else if($end_hours >= 13.5 && $end_hours <= 18.5){ //结束时间在13点30-18点30之间
							$timestamp += $start_hours_12 - $dt_start + ($dt_end - $start_hours_13_5);
						}else if($end_hours > 18.5){ //结束时间在18点30之后
							$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){ //结束时间在12点-13点30之间
							$timestamp += 0;
						}else if($end_hours >= 13.5 && $end_hours <= 18.5){ //结束时间在13点30-18点30之间
							$timestamp += $dt_end - $start_hours_13_5;
						}else if($end_hours > 18.5){ //结束时间在18点30之后
							$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){ //结束时间在13点30-18点30之间
							$timestamp += $dt_end - $dt_start;
						}else if($end_hours > 18.5){ //结束时间在18点30之后
							$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){ //开始时间小于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){ //结束时间小于9点
						$timestamp += 0;
					}else if($end_hours >= 9 && $end_hours <= 12){ //结束时间大于等于9点小于等于12点
						$timestamp += $dt_end - $end_hours_9;
					}else if($end_hours > 12 && $end_hours < 13.5){ //结束时间在12点-13点30之间
						$timestamp += $end_hours_12 - $end_hours_9;
					}else if($end_hours >= 13.5 && $end_hours <= 18.5){ //结束时间在13点30-18点30之间
						$timestamp += $end_hours_12 - $end_hours_9 + ($dt_end - $end_hours_13_5);
					}else if($end_hours > 18.5){ //结束时间在18点30之后
						$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;
	}

	//获取两个日期之间的工时,这里开始时间是2020-12-30,结束时间是2021-01-04,期间元旦1-3号不算工作日,不参与计时
	$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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值