这里是文章模块栏目内容页
php 计算某个日期时间段有几周 每周的开始日期和结束日期

在做数据统计的时候,经常涉及到按天统计数据,再按周统计数据;

那么计算 一个时间段 有几周,每周的开始和结束分别是 哪个日期,这就非常迫切需要得到计算结果;

下面这个算法是计算的php具体实现;

   //计算时间段每月的开始和结束日期
      function count_months($start_string, $end_string = '')
    {       
        $startDate = date('Y-m-d', strtotime($start_string) );
        if(empty($end_string)) {
            $endDate = date('Y-m-d');
        }else{
            $endDate = date('Y-m-d', strtotime($end_string));
        }
     
        $smonth = date("Ym",strtotime($startDate));
        $emonth = date("Ym",strtotime($endDate));
        if($smonth == $emonth){ // 同一月
            $s = date("Y-m-d",strtotime("first Day of this month 00:00:00", strtotime($startDate)));
            $e = date("Y-m-d",strtotime("last Day of this month 23:59:59", strtotime($endDate)));
            $monthList[] =array('s'=>$s, 'e'=> $e);        
        }else{
           
            $month_end    = date("Y-m-d",strtotime("first Day of next month 00:00:00", strtotime($startDate)));            
            $monthList[] =array('s'=>date("Y-m-d", strtotime("first Day of this month 00:00:00", strtotime($startDate))), 
                        'e'=> date("Y-m-d",strtotime("last Day of this month 23:59:59", strtotime($startDate))) ); 
          
            if($month_end >= $endDate){
                $monthList[] = array('s'=> strtotime("last Day of this month 23:59:59", strtotime($startDate)), 
                            'e'=> strtotime("last Day of this month 23:59:59", strtotime($endDate)));
            }else{
                while($month_end <= $endDate){
                    $start         = $month_end;
                    $month_end     = date("Y-m-d",strtotime("first Day of next month 00:00:00", strtotime($start)));
                    if($month_end <= $endDate){
                        $monthList[]  = array('s'=>$start, 'e'=>date("Y-m-d",strtotime("$month_end")));
                    }else{
                        $monthList[]  = array('s'=>$start, 'e'=>date("Y-m-d",strtotime("$endDate")));
                    }
                }
            }
        }
        return $monthList;
    }
//计算时间段每周的开始和结束日期
    function count_weeks($start_string, $end_string = '')
    {       
        $startDate = date('Y-m-d', strtotime($start_string) );
        if(empty($end_string)) {
            $endDate = date('Y-m-d');
        }else{
            $endDate = date('Y-m-d', strtotime($end_string));
        }
        
        $sw = date("Ymw",strtotime($startDate));
        $ew = date("Ymw",strtotime($endDate));
        if($sw == $ew){            
            $weekList[] =array('s'=>  date("Y-m-d",strtotime("this week 00:00:00", strtotime($startDate))), 'e'=> date("Y-m-d",strtotime("next week 00:00:00 -1second", strtotime($endDate))));        
        }else{
            
            $week_end    = date("Y-m-d",strtotime("next week 00:00:00  -1second", strtotime($startDate)));            
            $weekList[]  = array('s'=>date("Y-m-d", strtotime("this week 00:00:00", strtotime($startDate))), 
                        'e'=> date("Y-m-d",strtotime("next week 00:00:00  -1second", strtotime($startDate))) ); 
 
            if($week_end >= $endDate){
                $weekList[] = array('s'=> strtotime("this week 23:59:59", strtotime($startDate)), 
                'e'=> strtotime("next week 00:00:00", strtotime($endDate)));
               
            }else{ 
                while($week_end <= $endDate){
                    $start         = date("Y-m-d",strtotime(" +1 day", strtotime($week_end)));
                    $week_end     = date("Y-m-d",strtotime("next week 00:00:00  -1second", strtotime($start)));
                    if($week_end <= $endDate){
                        $weekList[]  = array('s'=>$start, 'e'=>date("Y-m-d",strtotime("$week_end")));
                    }else{
                        $weekList[]  = array('s'=>$start, 'e'=>date("Y-m-d",strtotime("next week 00:00:00  -1second",strtotime("$endDate") )));
                    }
                }
            }
        }
        return $weekList;
    }


上面就是 计算某个日期时间段有几周  每周的开始日期和结束日期 的php具体方法实现;

希望能帮助到您,感谢您的阅读;


更多栏目
相关内容