get recurring dates between two dates and periods php codeigniter

	public function getRecurringDates($start_date,$period,$weekdayNumbers = [])
	{
		$end_date = date("Y-m-d", strtotime("+$period days", strtotime($start_date)) );

	    $dateArr = array();

	    foreach ($weekdayNumbers as $key => $weekdayNumber) {


			$startDate = strtotime($start_date);
		    $endDate = strtotime($end_date);

	    	do
		    {
		        if(date("w", $startDate) != $weekdayNumber)
		        {
		            $startDate += (24 * 3600); // add 1 day
		        }
		    } while(date("w", $startDate) != $weekdayNumber);


		    while($startDate <= $endDate)
		    {
		        $dateArr[] = date('Y-m-d', $startDate);
		        $startDate += (7 * 24 * 3600); // add 7 days
		    }

	    }
		    
	    return($dateArr);
	}

        // die('lol');
	$event_id = (isset($_POST['event_id']))? $_POST['event_id'] : 331; 
	$start_date = (isset($_POST['event_start_date']))? $_POST['event_start_date'] : '2017-04-15';
	$period = (isset($_POST['event_period']))? $_POST['event_period'] : 60;
	$weekdayNumbers = (isset($_POST['event_id']) && is_array($_POST['event_id']))? $_POST['event_id'] : [0,2];

	$dateArr = $this->getRecurringDates($start_date, $period, $weekdayNumbers);
		

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s