php计算一段时间内有几个星期或月

php计算星期或月 php计算星期 php计算月 php计算一段时间内有几个星期或月

//先取开始日期所处的星期,然后计算时间段内有几个星期

$n = (strtotime($enddate)-strtotime($startdate))/86400;

//$endDate = date("Y-m-d",strtotime("$endDate +1 day"));

if($n<=7){

$day = date("w",strtotime($startdate))-1;

$week_start = date("Y-m-d 00:00:00",strtotime("$startdate -{$day} day"));

$day = 7-$day;

$week_end = date("Y-m-d 00:00:00",strtotime("$startdate +{$day} day"));

if($week_end >= $enddate){

$weekList[] =array($startdate,$enddate);

}else{

$weekList[] =array($startdate,$week_end);

$weekList[] =array($week_end,$enddate);

}

}else{

$day = date("w",strtotime($startdate))-1;

$week_start = date("Y-m-d 00:00:00",strtotime("$startdate -{$day} day"));

$day = 7-$day;

$week_end = date("Y-m-d 00:00:00",strtotime("$startdate +{$day} day"));

$weekList[] =array($startdate,$week_end);

if($week_end >= $enddate){

$weekList[] = array($week_end,$enddate);

}else{

while($week_end <= $enddate){

$start = $week_end;

$week_end = date("Y-m-d 00:00:00",strtotime("$week_end +7 day"));

if($week_end <= $enddate){

$weekList[] = array($start,$week_end);

}else{

$weekList[] = array($start,$enddate);

}

}

}

}




$month = date("Y-m",strtotime($startdate));

//月份第一天的日期

$first_date=$month.'-01';

//月份的天数

$count_days=date("t",strtotime($month));

//月份最后一天的日期

$last_date=date("Y-m-d 00:00:00",strtotime($first_date.($count_days-1)." day"));

if($enddate <= $last_date){

$month_list[] = array($startdate,$enddate);

}else{

$month_list[] = array($startdate,$last_date);

while($enddate > $last_date){

//下个月的第一天

$next_first_day = date("Y-m-d 00:00:00",strtotime($last_date."+1 day"));

//下个月月份

$next_month = date("Y-m",strtotime($next_first_day));

//下月月份的天数

$next_count_days=date("t",strtotime($next_month));

//下个月的最后一天

$last_date = date("Y-m-d 00:00:00",strtotime($next_first_day.($next_count_days-1)." day"));

if($enddate < $last_date){

$month_list[] = array($next_first_day,$enddate);

}else{

$month_list[] = array($next_first_day,$last_date);

}

}

}



评论