- /**
- * | Author: Saron.Mo <momosweb@qq.com>
- * | @param char|int $start_date 一个有效的日期格式,例如:20091016,2009-10-16
- * | @param char|int $end_date 同上
- * | @param int $weekend_days 一周休息天数
- * | @return array
- * | array[total_days] 给定日期之间的总天数
- * | array[total_relax] 给定日期之间的周末天数
- */
- function get_weekend_days($start_date, $end_date, $weekend_days=1) {
- $data = array();
- if (strtotime($start_date) > strtotime($end_date)) list($start_date, $end_date) = array($end_date, $start_date);
- $start_reduce = $end_add = 0;
- $start_N = date('N',strtotime($start_date));
- $start_reduce = ($start_N == 7) ? 1 : 0;
- $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;
- break;
- case 1:
- default:
- $end_add = ($end_N == 7) ? 1 : 0;
- break;
- }
- $days = round(abs(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;
- }
在原Author: Yang Yu <niceses@gmail.com>写的基础上优化了一下对总天数的计算,增加了单休工作日的计算。
搜索原出处:http://www.jb51.net/article/20886.htm
转载于:https://blog.51cto.com/saron/757899