1、获取每个月各周的开始与结束时间
/**
* 获取每个月每周开始与结束时间
*/
public function monthPerWeek(){
$today = date('Y-m-d',time());
$year = date('Y', strtotime($today)); //获取年
$month = date('m', strtotime($today)); //获取月
$day = date('d', strtotime($today)); //获取日
$j = date("t",strtotime($today)); //获取当前月份天数
for ($i=1; $i <$j ; $i=$i+7) { //循环本月有多少周
$w = date('N',strtotime($year.'-'.$month.'-'.$i)); //计算第一天是周几
//第一天
if($i == 1){
$start = strtotime($year.'-'.$month.'-'.$i);
}else{
$start = strtotime($year.'-'.$month.'-'.$i.' -'.($w-1).' days');
}
//最后一天
if($j-$i > 7){
$end = strtotime($year.'-'.$month.'-'.$i.' +'.($w).' days');
}else{
$end = strtotime($year.'-'.$month.'-'.$i.' +'.(7-$w).' days');
}
$weekinfo[$i][] = array(date('Y-m-d',$start),date('Y-m-d',$end));
}
return $weekinfo;
}
2、获取当前月份的各个周的开始时间
最后一天为下月的第一天的00:00:00
/**
* 本月每周开始与结束时间
*/
public function monthPerWeek(){
$time = time();
$j = date("t",$time); //获取当前月份天数
$week = array();
for ($i=1; $i <$j ; $i=$i+7) { //循环本月有多少周
$w = date('N',strtotime(date('Y-m-'.$i))); //计算第一天是周几
//第一天
if($i == 1){
$day = strtotime(date('Y-m-'.$i));
}else{
$day = strtotime(date('Y-m-'.$i).' -'.($w-1).' days');
}
$week[] = $day;
}
//最后一天
$week[] = strtotime(date('Y-m-'.$j) .'+ 1days');
return $week;
}