Schedule Repeat events

Discussion in 'PHP' started by assgar, Jul 22, 2007.

  1. #1
    Schedule Repeat events

    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.




    :cool:
     
    assgar, Jul 22, 2007 IP
  2. mrmonster

    mrmonster Active Member

    Messages:
    374
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #2
    If you are asking how to executed timed events in PHP the answer is you can't.

    Theres nothing build into PHP to schedule actions in the future, you need to use an external tool like crontab on a *nix based system. It will allow you to schedule when to execute scripts, you could then use PHP system() command to create and update the cron jobs.
     
    mrmonster, Jul 22, 2007 IP
  3. assgar

    assgar Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
    ?>
    
    PHP:
     
    assgar, Jul 28, 2007 IP