date_default_timezone_set('PRC');
function count_weeks($startDate, $endDate = ''){
$startDate = date('Y-m-d', $startDate);
if(empty($endDate)) {
$endDate = date('Y-m-d');
}else{
$endDate = date('Y-m-d', $endDate);
}
$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",strtotime("$startDate -{$day} day"));
$day = 7-$day;
$week_end = date("Y-m-d",strtotime("$startDate +{$day} day"));
if($week_end>=$endDate){
$weekList[] =array('s'=>$startDate, 'e'=>date("Y-m-d",strtotime("$endDate -1 day")));
}else{
$weekList[] =array('s'=>$startDate, 'e'=>date("Y-m-d",strtotime("$week_end -1 day")));
$weekList[] =array('s'=>$week_end, 'e'=>date("Y-m-d",strtotime("$endDate -1 day")));
}
}else{
$day = date("w",strtotime($startDate))-1;
$week_start = date("Y-m-d",strtotime("$startDate -{$day} day"));
$day = 7-$day;
$week_end = date("Y-m-d",strtotime("$startDate +{$day} day"));
$weekList[] =array('s'=>$startDate, 'e'=>date("Y-m-d",strtotime("$week_end -1 day")));
if($week_end >= $endDate){
$weekList[] = array('s'=>$week_end, 'e'=>date("Y-m-d",strtotime("$endDate -1 day")));
}else{
while($week_end <= $endDate){
$start = $week_end;
$week_end = date("Y-m-d",strtotime("$week_end +7 day"));
if($week_end <= $endDate){
$weekList[] = array('s'=>$start, 'e'=>date("Y-m-d",strtotime("$week_end -1 day")));
}else{
$weekList[] = array('s'=>$start, 'e'=>date("Y-m-d",strtotime("$endDate -1 day")));
}
}
}
}
return $weekList;
}