0
votes

I have a complete dates of the year, for example here is the code i took dates for complete year by passing particular day start date, end date , day and weekday as "2016-01-01","2016-12-31","Sunday", 0

PHP CODE:

 $start_date = "2016-01-01";
 $end_date   = "2016-12-31";
 $day        = "Sunday";

 //weekday: Sunday = 0 , Monday = 1 ..only if it is mulitple days
 $dateArr = GetSelectdayDatesForCurYear($start_date, $end_date,$day, 0); 

 function GetSelectdayDatesForCurYear($start_date, $end_date, $selected_day, $weekday)
    {     
        $seltd_weekdays  = $selected_day;  //even can supply multiple days like "Sunday,Monday,Friday..";   
        $arr_weekdays    = explode(",", $seltd_weekdays);   
        $weekdaynum      = $arr_weekdays[$weekday];

        if(!$weekdaynum){  //if Sunday[0],Monday[1] not available
                echo "Invalid Weekday!";
        }
        else
        {   
            $get_start_date  = strtotime("+0 day", strtotime($start_date) );
            $get_end_date    = strtotime($end_date);

            $dateArr  = array();
            $get_date = strtotime($weekdaynum, $get_start_date);  //first date for the first month

            while($get_date <= $get_end_date) //loop till end date
            {
                    $dateArr[] = date("Y-m-d", $get_date);
                    $get_date  = strtotime("+1 weeks", $get_date);      
            }
            $dateArr[] = date("Y-m-d", $get_date);
            return $dateArr;
        }
    }

Here now i am trying to pass week no, example i need all the dates of sunday's for 1st week of complete year then i need the dates of first week or if i need 1st,3rd week, i need to get complete dates for 1,3 week, by above code i got complete dates for the year, how to get by week basics.

example i am trying to supply weeks number like this:

 $for_the_weeks  =  "1,2";  //or "1,3,5" or "1,2,4"

How to make it out !!!

2
AOA, khalid, i have sent a solution plz check.devpro

2 Answers

1
votes

You can get all sunday from first and second months by using this method:

// create an array for allowed week as you need 1,2 (use as first,second for )
$allowedWeek = array('first','second');

// this loop will execute two times.
foreach ($allowedWeek as $key => $value) 
{
    for($i=1;$i<=12;$i++)
    {
        // start from and end 
        $start = new DateTime(''.$value.' sunday of 2016-'.$i);
        $end   = new DateTime('2016-12-31');

        $interval = DateInterval::createFromDateString(''.$value.' sunday of next month');
        $period = new DatePeriod($start, $interval, $end);

        // store date by month wise in an array
        $dateArr[$value][] = $period->start->format('Y-m-d');
    } 
}

echo "<pre>";
print_r($dateArr);

Result is:

Array
(
    [first] => Array
        (
            [0] => 2016-01-03
            [1] => 2016-02-07
            [2] => 2016-03-06
            [3] => 2016-04-03
            [4] => 2016-05-01
            [5] => 2016-06-05
            [6] => 2016-07-03
            [7] => 2016-08-07
            [8] => 2016-09-04
            [9] => 2016-10-02
            [10] => 2016-11-06
            [11] => 2016-12-04
        )

    [second] => Array
        (
            [0] => 2016-01-10
            [1] => 2016-02-14
            [2] => 2016-03-13
            [3] => 2016-04-10
            [4] => 2016-05-08
            [5] => 2016-06-12
            [6] => 2016-07-10
            [7] => 2016-08-14
            [8] => 2016-09-11
            [9] => 2016-10-09
            [10] => 2016-11-13
            [11] => 2016-12-11
        )

)
0
votes

Complete Answer :

Here is the answer for my question. I derived the answer by above answered code, thanks for 'devpro' to help me out!

    $selected_leave_weeks = array(1,2,3,4,5); // array(1,2,3,4) or array(1,5) etc;

    //Change the week format to week name for supplying as array to the function.        
    $week_array = array();
    foreach($selected_leave_weeks as $convert_week)
    {            
        switch ($convert_week)
        {
            case 1:
            $convert_week = 'first';
            $week_array[] = $convert_week;
            break;

            case 2:
            $convert_week = 'second';
            $week_array[] = $convert_week;
            break;

            case 3:
            $convert_week = 'third'; 
            $week_array[] = $convert_week;
            break;

            case 4:
            $convert_week = 'fourth';
            $week_array[] = $convert_week;
            break;

            case 5:
            $convert_week = 'last'; 
            $week_array[] = $convert_week;
            break;
        }
    }

    //Supply Week names for fetching complete dates you need - supplying weeks as array('first','second','third','fourth','last')

    //This gives a complete dates for whole year
    function getDateForSpecificWeeks($selected_day, $cur_year, $allowedWeek)
    {
        foreach ($allowedWeek as $key => $value) 
        {
            for($i=1;$i<=12;$i++)
            {
                // start from and end 
                $start = new DateTime(''.$value.' '.$selected_day.' of '.$cur_year.'-'.$i);
                $end   = new DateTime(''.$cur_year.'-12-31');  // last day is always December 31

                $interval = DateInterval::createFromDateString(''.$value.' '.$selected_day.' of next month');
                $period = new DatePeriod($start, $interval, $end);

                // store date by month wise in an array
                $dateArr[] = $period->start->format('Y-m-d');
            } 
        }
        return $dateArr;     
    }

    //Supply Parameters for getting dates        
    $allowed_weeks  = $week_array; //From Above code - $week_array contain array('first','second','third','fourth','last')        
    $selected_day   = "Sunday";//Sunday, Monday, ..etc
    $cur_year       = "2016";//date('Y');   //Takes current Year

    //if 4th week is not selected remove dates from 5th Week- "last" mean { because its give last date of the month}
    if ((in_array("5", $selected_leave_weeks)) && (!in_array("4", $selected_leave_weeks)))
    {            
        $fourth_week             =  array('fourth');
        $fourth_week_dates       = getDateForSpecificWeeks($selected_day, $cur_year, $fourth_week);     

        $get_config_week_dates   = getDateForSpecificWeeks($selected_day, $cur_year, $allowed_weeks);
        //array_diff removes 4th week dates contain in 5th
        $dates_avail             = array_diff($get_config_week_dates, $fourth_week_dates);
        //remove a date if dates are twice or more
        $datesArr                 = array_unique($dates_avail); 
    }
    else
    {  
        $get_config_week_dates   = getDateForSpecificWeeks($selected_day, $cur_year, $allowed_weeks);   
        //remove one date if dates are twice or more
        $datesArr                 = array_unique($get_config_week_dates);
    }

    //you will get your date
    echo '<pre>';
    print_r($datesArr);
 ?>

Result :

     Array
        (
            [0] => 2016-01-03
            [1] => 2016-02-07
            [2] => 2016-03-06
            [3] => 2016-04-03
            [4] => 2016-05-01
            [5] => 2016-06-05
            [6] => 2016-07-03
            [7] => 2016-08-07
            [8] => 2016-09-04
            [9] => 2016-10-02
            [10] => 2016-11-06
            [11] => 2016-12-04
            [12] => 2016-01-10
            [13] => 2016-02-14
            [14] => 2016-03-13
            [15] => 2016-04-10
            [16] => 2016-05-08
            [17] => 2016-06-12
            [18] => 2016-07-10
            [19] => 2016-08-14
            [20] => 2016-09-11
            [21] => 2016-10-09
            [22] => 2016-11-13
            [23] => 2016-12-11
            [24] => 2016-01-17
            [25] => 2016-02-21
            [26] => 2016-03-20
            [27] => 2016-04-17
            [28] => 2016-05-15
            [29] => 2016-06-19
            [30] => 2016-07-17
            [31] => 2016-08-21
            [32] => 2016-09-18
            [33] => 2016-10-16
            [34] => 2016-11-20
            [35] => 2016-12-18
            [36] => 2016-01-24
            [37] => 2016-02-28
            [38] => 2016-03-27
            [39] => 2016-04-24
            [40] => 2016-05-22
            [41] => 2016-06-26
            [42] => 2016-07-24
            [43] => 2016-08-28
            [44] => 2016-09-25
            [45] => 2016-10-23
            [46] => 2016-11-27
            [47] => 2016-12-25
            [48] => 2016-01-31
            [52] => 2016-05-29
            [54] => 2016-07-31
            [57] => 2016-10-30
        )