Generate Calender weeks between two given dates

/*
   * This Function Help to Genrate Weeks Between Two dates....
   */
function _genWeek($start,$end){
   
    $increment=1;
    $startdate = strtotime($start);
    $enddate = strtotime($end); 
    $day=date("w", $startdate);
    $day1=date("w",$enddate );
    $current_week = mktime(0,0,0,date("m",$startdate),date("d",$startdate),date("Y",$startdate));
    $current_week1 = mktime(0,0,0,date("m",$startdate),date("d",$startdate)-$day,date("Y",$startdate));
    $all_weeks = array();
    $keyDate=0;
   
    if($day<$day1){
        $diff=((($enddate-$current_week)/86400)/7);
       
    }else{
        $diff=ceil((($enddate-$current_week)/86400)/7);
       
    }
   
    if($startdate==$enddate){
        $all_weeks[0]['start'] =$startdate ;
        $all_weeks[0]['end'] =$enddate;
       
    }else{
       
        $arrDay = array('Mon'=>5,'Tue'=>4,'Wed'=>3,'Thu'=>2,'Fri'=>1,'Sat'=>0,'Sun'=> 6);
        for ($week = 0; $week <=$diff; $week += $increment)
            {
                $week_data = array();
                if($week == 0){
                   
                   
                    $week_data['start'] = strtotime("+$week weeks", $current_week);
                    $Days = $arrDay[date("D",$week_data['start'])];
                    $week_data['end'] = strtotime("+".$Days." days", $week_data['start']);
                }
                else{
                    $week_data['start'] = strtotime("+$week weeks", $current_week1);
                    $week_data['end'] = strtotime("+6 days", $week_data['start']);
                }
                $keyDate = $week + 1;
                $all_weeks[$keyDate] = $week_data;
            }
        $date1 = $enddate;
        $date2= $all_weeks[$keyDate]['end'] ;
        if($date2!=$date1){
            $diffDate = abs($date2 - $date1);
            $LD =  date('d',$diffDate);
            $LD=$LD-1;
            $latestDate = strtotime("-".$LD." days",$all_weeks[$keyDate]['end']);
            $all_weeks[$keyDate]['end'] = $latestDate;
           
        }
       
        for ($week = 0; $week <=$diff; $week += $increment)
            { $secLast = $all_weeks[$week]['end'];
                $last = $all_weeks[$week+1]['end'];
                if($secLast == $last){
                    unset($all_weeks[$week+1]);
                }
            }
    }
    return  $all_weeks;
   
  }

Tags: 
X