获取某年周、月、季度的开始日期和结束日期

/**
 * 计算一年有多少周,每周从星期一开始,
 * 如果最后一天在周四后(包括周四)算完整的一周,否则不计入当年的最后一周
 * 如果第一天在周四前(包括周四)算完整的一周,否则不计入当年的第一周
 * @param int $year
 * return int
 */
function week($year){
    $year_endday = mktime(0,0,0,12,31,$year);
    if (intval(date('W',$year_endday)) === 1){
        return date('W',strtotime('last week',$year_endday));
    }else{
        return date('W',$year_endday);
    }
}
/**
 * 获取某年第几周的开始日期和结束日期
 * @param int $year
 * @param int $week 第几周;
 */
function weekday($year,$week=1){
    $year_start = mktime(0,0,0,1,1,$year);
    $year_end = mktime(0,0,0,12,31,$year);

    // 判断第一天是否为第一周的开始
    if (intval(date('W',$year_start))===1){
        $start = $year_start;//把第一天做为第一周的开始
    }else{
        $week++;
        $start = strtotime('+1 monday',$year_start);//把第一个周一作为开始
    }

    // 第几周的开始时间
    if ($week===1){
        $weekday['start'] = $start;
    }else{
        $weekday['start'] = strtotime('+'.($week-0).' monday',$start);
    }

    // 第几周的结束时间
    $weekday['end'] = strtotime('+1 sunday',$weekday['start']);
    if (date('Y',$weekday['end'])!=$year){
        $weekday['end'] = $year_end;
    }
    return $weekday;
}
/**
 * 获取某年第几月的开始日期和结束日期
 * @param int $year
 * @param int $month 第几月;
 */
function monthday($year,$month){
    $firstday = date('Y-m-01', mktime(0,0,0,$month,1,$year)); //月初
    $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
    return [
        'firstday' => $firstday,
        'lastday' =>$lastday
    ];
}
/**
 * 获取某年第几季度的开始日期和结束日期
 * @param int $year
 * @param int $season 第几季度;
 */
function seasonday($year,$season){
    $firstday = date('Y-m-01',mktime(0,0,0,($season - 1) *3 +1,1,$year));
    $lastday = date('Y-m-t',mktime(0,0,0,$season * 3,1,$year));
    return [
        'firstday' => $firstday,
        'lastday' =>$lastday
    ];
}
/**
 * 获取周几
 * @param int $date
 */
function getdateweek($date){
    $weekarray=array("周日","周一","周二","周三","周四","周五","周六");
    return $weekarray[date("w",strtotime($date))];
}

 

转载于:https://www.cnblogs.com/zhouguowei/p/9262588.html

你可能感兴趣的:(获取某年周、月、季度的开始日期和结束日期)