1、周转日,全年第n周的起止日
public function dayFromWeek($week,$year=0){
$year = empty($year)?date('Y'):$year;
$year_start = mktime(0,0,0,1,1,$year);
// 判断第一天是否为第一周的开始
if (1 == date('W',$year_start)){
$start = $year_start;//把第一天做为第一周的开始
}else{
$start = strtotime('next Monday', $year_start);
}
// 第几周的开始时间
if (0 == $week){
$weekday['start'] = strtotime('last Monday', $year_start);
}elseif (1 == $week){
$weekday['start'] = $start;
}else{
$weekday['start'] = strtotime('+'.($week-0).' monday',$start);
}
// 第几周的结束时间
$weekday['end'] = strtotime('+1 sunday',$weekday['start']);
$weekday['start'] = date("Y-m-d",$weekday['start']);
$weekday['end'] = date("Y-m-d",$weekday['end']);
return $weekday;
}
2、月起止日
public function dayFromMonth($month, $year=0){
$year = empty($year)?date('Y'):$year;
$month = strlen($month)==2?$month:'0'.$month;
$startDay = $year.'-'.$month.'-01';
$endDay = date('Y-m-d', strtotime(date('Y-m-01', strtotime($startDay)) . ' +1 month -1 day'));
return ['start'=>$startDay, 'end'=>$endDay];
}
3、月起止周,当月跨过了哪些周
public function weekFromMonth($month, $year=0){
$day = $this->dayFromMonth($month, $year);
return ['start'=>date('W',strtotime($day['start'])),'end'=>date('W',strtotime($day['end']))];
}
4、第几周在当月的起止日
public function dayFromWM($week,$month,$year=0){
$year = empty($year)?date('Y'):$year;
$year_start = mktime(0,0,0,1,1,$year);
$year_end = mktime(0,0,0,12,31,$year);
$month_start = $year.'-'.$month.'-01';
$month_end = date('Y-m-d', strtotime(date('Y-m-01', strtotime($month_start)) . ' +1 month -1 day'));
// 判断第一天是否为第一周的开始
if (intval(date('W',$year_start))===1){
$start = $year_start;//把第一天做为第一周的开始
}else{
$start = strtotime('next Monday', $year_start);
}
// 第几周的开始时间
if ($week===0){
$weekday['start'] = strtotime('last Monday', $year_start);
}elseif ($week===1){
$weekday['start'] = $start;
}else{
$weekday['start'] = strtotime('+'.($week-0).' monday',$start);
}
// 第几周的结束时间
$weekday['end'] = strtotime('+1 sunday',$weekday['start']);
if (date('m',$weekday['end'])!=$month){
$weekday['end'] = $month_end;
}else{
$weekday['end'] = date("Y-m-d",$weekday['end']);
}
if (date('m', $weekday['start'])!=$month){
$weekday['start'] = $month_start;
}else{
$weekday['start'] = date("Y-m-d",$weekday['start']);
}
return $weekday;
}