Check which time slots are available

i am having a time slots

9:00 - 9.30
9:30 - 10:00
10:00-10:00
:
5:00- 5:30

i want to show that which slots are available and which is not available.

if available then it will be blue in color other wise red.

What do you have for code so far?

<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	 $p_name = $_POST['p_name'];
	
	
 $booking_start_time          = "09:30";			// The time of the first slot in 24 hour H:M format  
 $booking_end_time            = "19:00"; 			// The time of the last slot in 24 hour H:M format  
 $booking_frequency           = 30;   			// The slot frequency per hour, expressed in minutes.  	

// Day Related Variables

 $day_format					= 1;				// Day format of the table header.  Possible values (1, 2, 3)   
															// 1 = Show First digit, eg: "M"
															// 2 = Show First 3 letters, eg: "Mon"
															// 3 = Full Day, eg: "Monday"
	
 $day_closed					= array("Saturday", "Sunday"); 	// If you don't want any 'closed' days, remove the day so it becomes: = array();
 $day_closed_text				= "CLOSED"; 		// If you don't want any any 'closed' remove the text so it becomes: = "";


echo "
	<div id='outer_booking'><h2>Available Slots</h2>

	<p>
	The following slots are available on  $p_name
	</p>
	
	<table width='400' border='0' cellpadding='2' cellspacing='0' id='booking'>
		<tr>
			<th width='150' align='left'>Start</th>
			<th width='150' align='left'>End</th>
			
			<th width='20' align='left'>Book</th>			
		</tr>
		<tr>
			<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>
		</tr>";
				

		// Create $slots array of the booking times
		for($i = strtotime($booking_start_time); $i<= strtotime($booking_end_time); $i = $i + $booking_frequency * 60) {
			$slots[] = date("H:i:s", $i);  
		}			
				

		// Loop through $this->bookings array and remove any previously booked slots
		// Close if
		
				
		
		// Loop through the $slots array and create the booking table
		
		foreach($slots as $i => $start) {			

			// Calculate finish time
			$finish_time = strtotime($start) + $booking_frequency * 60; 
		
			echo "
			<tr>\r\n
				<td>" . $start . "</td>\r\n
				<td>" . date("H:i:s", $finish_time) . "</td>\r\n
				
				<td width='110'><input data-val='" . $start . " - " . date("H:i:s", $finish_time) . "' class='fields' type='checkbox'></td>
			</tr>";
		
		} // Close foreach			
	
		echo "</table></div><!-- Close outer_booking DIV -->";
}
?>

How do you determine whether a time-slot is available? Does that come from a database table and, if so, what is the structure of that table?

yes it will come from the database
having a field from_date date
from_time time
to_time time

I think the way I’d do it is to write a query to retrieve the appointments from the database and stick the start and end times into an array, then I’d have a function which accepts the start and end times of each time slot and scans down the array to see whether that time slot is already occupied. That function would return true or false and you could use that to decide what colour to display.

The task is less complicated if your time slots are always based on regular start and end times - if they’ll always be starting on the hour or half-hour as your code suggests, you could optimise that by just encoding each half-hour period in an array indexed 0-47, which would make it really easy to check. The problem with that approach is when the user decides they’d like to go for fifteen-minute slots, or seven-minute ones, and different-length slots for different types of appointment.

it will be helpfull if u kindly show me a rough code…

$sql = "select * from mytable where date=$selected-date";
$res = $db->query($sql);

while ($row = $res->fetch()) {
  $start = $row['from_time'];
  $end = $row['to_time'];
  $st = timetoslot($start);
  $end = timetoslot($end);
  for ($x = $st; $x < $end; $x++ ) {
    $slot[$x] = "*"; }
  }

The timetoslot() function takes a time within a day, and converts it to an integer. You’ll have to write that as I can’t remember off the top of my head how to do it in PHP. Basically what you’re trying to do is to convert actual appointment times into integers. So if you think of the day as a series of time slots:

Slot1 = 00:00 to 00:29
Slot2 = 00:30 to 00:59
Slot3 = 01:00 to 01:29

and so on, that function simply takes the time passed in, converts it to seconds, divides it by a magic number (your $booking_frequency variable, probably) and returns that number. So you can then translate a time into a slot number. Within the loop, then, we stick a value inside the array for that slot number which could just be a star as above to indicate “booked”, or could have details about what or who the slot is booked for.

So when you display the time slots in your calendar, all you need to do is again call timetoslot() to get the slot-number, then you can look in the array and see if it’s occupied or not.

1 Like

thank you… :blush:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.