Jump to content

[SOLVED] Schedule Repeat events


assgar

Recommended Posts

 

 

Hi

 

I am stuck in fact I don't know where to start, can you point

me in the right direction?

 

I have the form to capture the user input but don't

know how to proceed to use this information.

 

 

 

I need to allow the user to schedule repeated events for

the same time, for the same day between two time periods.

Ideally it would be great to check for conflicts.

 

 

Note: The info recorded by the form is listed below.

 

A) The start date and time is determine by the first event

 

B) The user selects a comdination of 1) interval 2) frequency and

  3) week day to determine when the repeating event will occur:

 

  1) interval: 

      Daily, 

      Weekly,

      Monthly,

      Annually

 

  2) frequency:

    Every  

    Every Other  

    Every 3rd  

    Every 4th  

    Every 5th  

    Every 6th  

 

 

  3) Week Day:

    Monday

    Tuesday

    Wednesday

    Thursday

    Friday

    Saturday

    Sunday

             

C) The end date (YYYYMMDD) is then selected.           

             

             

 

             

Note: I have the code to insert the results into the mysql database using PHP.

     

     

   

   

 

Link to comment
Share on other sites

Probably no simple solution to this, but i think what you would have to do is to generate timestamps for each time the event occurs - you can do this since you know the start date, end date, and when it will occur in between.

 

For instance, if i wanted to shedule an event every other day for the next 30 days - then you would have to generate 15 start times and 15 corresponding end times(so, for example, if i my event was 1 hour - each end time would be 1 hour after the start)

 

I think you could achieve the creating of the timestamps through a combination of the date() and strtotime() functions.

 

You could then insert each of these pairs of timestamps into the database. When checking for clashes, you would check the start and end time to see if there is already an event that falls on that time.

Link to comment
Share on other sites

Hi

 

Thanks for the assistance your effort was appreciated.

 

The final code can be found below.

 

This bit of code insert events into a database with these functionalities:

1) Single or multiple time blocks

2) Repeat event booking

3) Checks for event time conflicts

  Note: if single a event is being booked message is sent to user

  else if multiple repeat booking conflicts are store in

  table for user review and change event to another day or time.

 

I have only provided a quarter of the code so I hope I have not missed

anything related to this topic.

 

<?php 


   /**
    Note: form provide these parameters:
    	$start_date
    	$new_interval
    	$new_frequency
    	$end_date  //if no end_date use start date
    	$time_block //each time block is 10 or 15 min, this indicates the number of blocks 1 to 10

    
    Note: determine settings manually or use database stored configuration  
    	$double_book == "Y"; //event double booking
$repeat == "no";//if no end_date use start date and repeat is no else repeat is yes
    **/

/*******Note:
        - array repeatEvent(int $startTime, str $interval, int $frequency, int $endTime)
    	  returns array of UNIX times
   	- $startTime and $endTime must be valid UNIX time integer values
   	- $interval must be (case-insensitive): 'day', 'week', 'month', or 'year'
   	- $frequency must be positive integer (1 = every, 2, = every other, 3 = every 3rd, 4 = every 4th. 5 = every 5th, 6 = every 6th)
*********/

function repeatEvent($startTime, $interval, $frequency, $endTime)
   {
   		//make sure all paramters are valid
   		$startTime = (int) $startTime;
   		$endTime = (int) $endTime;

	if($startTime == 0)
  		  {
      		       user_error("repeatEvent(): invalid start time");
       			return(FALSE);
   		  }
  	  
   		if($endTime < $startTime)
   		  {
  		       user_error("repeatEvent(): invalid end time");
    		  }
 	         
   		$interval = strtolower(trim($interval));
   		if(!in_array($interval, array('day','week','month','year')))
   		  {
      	   	      user_error("repeatEvent(): Invalid interval '$interval'");
           	      return(FALSE);
        	  }

   		$frequency = (int)$frequency;
  		if($frequency < 1)
   		  {
      	     	      user_error("repeatEvent(): Invalid frequency '$frequency'");
      	     	      return(FALSE);
   		  }

   		$schedule = array();
   		for($time = $startTime; $time <= $endTime; $time = strtotime("+$frequency $interval", $time))
   		   {
      		      $schedule[] = $time;
   		   }
   		return($schedule);
  }


     /**-----------------------insert appointment-----------------------**/
     $sched = repeatEvent(strtotime($start_date), $new_interval, $new_frequency, strtotime($end_date));


     //outer loop repeated inserts
     foreach($sched as $date)
         {
      
      //inner loop the number of time blocks
      for($i = 0, $eTime = strtotime($event_time); $i < $time_block; 
    	      $i++, $eTime = strtotime("+$event_length minutes", $eTime)) 
	  { 
   			$new_event_time = date('H:i', $eTime); //increment time for new single or multi block event 
		$new_event_date = date('Y-m-d', $date);//increment date for single or repeat event

		/**-----------------------check conflicts------------------------**/
   			//determine the number of appointments for date and time to determine conflicts
		$event_count = "SELECT count(event_id) 
				FROM cal_appointment
				WHERE event_date = '$new_event_date'
				AND event_time = '$new_event_time '";
				$count = mysqli_query ($mysqli, $event_count);
				while($row = mysqli_fetch_array($count))
					{
						list($book_count) = $row;
					}							

		 /**----------- tables for inserting information-----------**/
		 /**Note: I have removed table fields to simplify example **/

		 //insert if conflict insert in to cal_repeat_conflict
		 $conflict_query = "INSERT INTO cal_repeat_conflict()
		 		    VALUES()";

		//no conflicts insert into cal_appointment table 
		$cal_query = "INSERT INTO cal_appointment()
				    VALUES();


		/**------------------------evaluate event time conflict---------------------**/
		//check to ensure that only allowed number of bookings for time slot is  entered
		if($double_book == "N" && $book_count == 1 && $repeat == "no")
  		     {
 	  		 event_conflict_message();//event time conflict assigned
 	  	     }
      			     elseif($double_book == "Y" && $book_count == 2 && $repeat == "no")
      	 			{
		  	    event_conflict_message();//event time conflict assigned
			}
			elseif($double_book == "N" && $book_count == 1 && $repeat == "yes")
  			    {
 	  			//insert into table event if time conflict
				mysqli_query($mysqli, $conflict_query)or die(mysqli_error($mysqli));
  			    }
      				    elseif($double_book == "Y" && $book_count == 2 && $repeat == "yes")
      	 				{
			 	   //insert into table event if time conflict
				   mysqli_query($mysqli, $conflict_query)or die(mysqli_error($mysqli));
				}							
				else
				    {
      						//no conflicts insert into cal_appointment table 
      						mysqli_query($mysqli, $cal_query)or die(mysqli_error($mysqli));
      					    }
      		}//end for loop
         }//end foreach loop
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.