一个比较有意思的函数,可以实现计算任意时间区间内工作日的函数。
这样你就能很方便的计算你每年的工作日有多少了。
/** * @param $start_date 开始日期 * @param $end_date 结束日期 * @param $weekend_days 一周休息天数 * * @return array */ function getWeekendDays($start_date, $end_date, $weekend_days=1) { $data = array(); $start_reduce = $end_add = 0; $start_N = date('N',strtotime($start_date)); $end_N = date('N',strtotime($end_date)); // 进行单、双休判断,默认按单休计算 $weekend_days = intval($weekend_days); switch ($weekend_days) { case 2: in_array($end_N,array(6,7)) && $end_add = ($end_N == 7) ? 2 : 1; $start_reduce = ($start_N == 7) ? 1 : 0; break; case 1: default: $end_add = ($end_N == 7) ? 1 : 0; break; } $days = ((strtotime($end_date) - strtotime($start_date))/86400) + 1; $data['total_days'] = $days; $data['total_relax'] = floor(($days + $start_N - 1 - $end_N) / 7) * $weekend_days - $start_reduce + $end_add; return $data; } echo "Two days per week\n"; var_dump( getWeekendDays("2022-01-01","2022-12-31",1));
转载请注明:苏demo的别样人生 » php 计算区间时间内工作日的函数