PHP计算指定时间段内周末天数(星期日天数)、双休天数、总工作日天数

/** 
 * | @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; 
}

你可能感兴趣的:(PHP,php,开发语言)