php 日历

/*
 * 参数param_begin_week 日历以周几开始 周日 0  周一到周六为1-6
 * */
function days($day_start_str, $day_end_str, $param_begin_week = 0)
{
    $timestamp_start = is_integer($day_start_str) ? $day_start_str : strtotime($day_start_str);
    $timestamp_end = is_integer($day_end_str) ? $day_end_str : strtotime($day_end_str);
    $year_start = date("Y", $timestamp_start);
    $month_start = date("n", $timestamp_start);
    $year_end = date("Y", $timestamp_end);
    $dates = [];
 
    for ($year = $year_start; $year <= $year_end; ++$year) {
        if ($year < $year_end) {
            $month_end = 12;
        } else {
            $month_end = date("n", $timestamp_end);
        }
        for ($month = $month_start; $month <= $month_end; ++$month) {
            $day_end = date("t", mktime(0, 0, 0, $month, 1, $year));
 
            //1号之前位置补空
            $week_of_first = date("w", mktime(0, 0, 0, $month, 1, $year));
            $space_num = $week_of_first - $param_begin_week;
            $space_num = $space_num >= 0 ? $space_num : 7 + $space_num;
            for ($s = 0; $s < $space_num; $s++) {
                $dates[$year][$month][] = '';
            }
            //当月日期
            for ($day = 1; $day <= $day_end; ++$day) {
                $dates[$year][$month][] = $day;
            }
            //当月最后一天之后位置补空
            $week_of_last = date("w", mktime(0, 0, 0, $month, $day_end, $year));
            $space_num = $param_begin_week - $week_of_last + -1;
            $space_num = $space_num >= 0 ? $space_num : 7 + $space_num;
            for ($s = 0; $s < $space_num; $s++) {
                $dates[$year][$month][] = '';
            }
        }
        $month_start = 1;
    }
    return $dates;
}
posted @ 2016-11-28 20:57  caroline2016  阅读(131)  评论(0编辑  收藏  举报