求某月/某年的工作日(排除周末,寒暑假,过节)

/**
 * 求某月/某年的工作日
 * @param  array $search 搜索类型
 * @param  array $other_day 寒暑假和过节
 * @return array            工作日
 */
function get_workday($search = array(), $other_day = array())
{
    //获取搜索的所有天数
    //获取某月中每天
    if (isset($search['month'])) {
        if ((int)$search['month'] < 1 || (int)$search['month'] > 12) {
            return false;
        }
        //转换为时间戳
        $start_date = strtotime($search['year'].'-'.$search['month'].'-01');
        //判断是否求最后一个月
        if ((int)$search['month'] == 12) {
            
            $end_date = strtotime(($search['year']+1).'-01-01');
        }
        else {
            $end_date = strtotime($search['year'].'-'.($search['month']+1).'-01');
        }
    } 
    //获取某年中每天
    elseif(isset($search['year'])) {
        $start_date = strtotime($search['year'].'-01-01');
        $end_date = strtotime(($search['year']+1).'-01-01');
    }

    $each_date = $start_date;
    while($each_date < $end_date) {
        $dates[] = $each_date;
        $each_date = strtotime('+1 day', $each_date);
        //或者 $each_date += $each_date+86400;
    }
    echo count($dates)."\n";

    // 1.排除周六周日
    $n = 0;
    foreach ($dates as $key => $timestamp) {
        $date_w = date('w', $timestamp);
        if ($date_w == 0 || $date_w == 6) {
            unset($dates[$key]);
        }
        $n++;
    }

    echo count($dates)."\n";

    // 2.排除节假日,过节
    $dates = array_diff($dates, $other_day);
    return $dates;
}

$search1 = array(
    'year' => '2015',
);
$search2 = array(
    'year' => '2014',
    'month' => '12'
);
get_workday($search2);exit;
posted @ 2015-01-27 09:38  yo胡yo  阅读(252)  评论(0编辑  收藏  举报