Bug in Loop variable which i cant find how to fix. Any help please!

Hi
I’m pulling my hair out having spent around 10 hours trying to fix this if it is fixable at all.
My script is a calender booking system which shows available slots through the day in the duration of time ie 20 mins a slot. It uses an hour counter and minute counter and checks with the db to see if the time is free or booked. This works all fine until you change the duration so the minute counter goes up in 20 min intervals or 15 minutes etc.

Lets say we have a 15 minute slot running, ie 0, 15, 30, 45, 60, 15
I have a booking 9 to 9:50 the next available slot would be 9:50 so I need to make the minute counter change to this time in minutes and not keeping going up in 15 minute increments, I have added this in but it all goes wrong. Very difficult to explain, minutes part of the code is below with error.
Full Code is below that.

Any help would be great
Steve

for($m=$mins; $m<60; $m++)
{
//echo(“Minutes:$m<br>”);
$minutes = $m;
$timeStamp = “$dateNum $dateMonth $dateYear $h:$minutes GMT”;
$unix = strtotime($timeStamp);
$unixSlotTimeLength = $unix+(60*$duration)-60;

		$bookedStatus="";
		$availableStatus  = "";
		
		
		//check there is a booking
		$query = "SELECT slotStatus FROM tblTimeSlots WHERE slotTime&gt;='$unix' AND slotTime&lt;='$unixSlotTimeLength' AND timeSlotID=1 ORDER BY id ASC";
		//echo ("$query&lt;br&gt;");
		$result = mysql_query($query,$db);
		while($row = mysql_fetch_array($result))
			{
			$status = $row[slotStatus];
			
				if($status=="BOOKED")
					{
					$bookedStatus ="BOOKED";
					}
				elseif($status=="AVAILABLE")
					{
					$availableStatus = "AVAILABLE";
					}
				else
					{
					$availableStatus = "NOT AVAILABLE";
					}
				}
				
		
		
		if($availableStatus=="AVAILABLE" && $bookedStatus!="BOOKED")
			{
			 $timeStamp = "$dateNum $dateMonth $dateYear $h:$minutes GMT";
			
					if($minutes==0)
						{
						$minutes="00";
						}
				$showTime = gmdate("g:i",$unix);
				echo("&lt;a href='/test5c.php?createAppointment=Y&slotFromTime=$unix&slotToTime=$unixSlotTimeLength&clientID=$clientID&staffID=$staffID&timeSlotID=$timeSlotID'&gt;$showTime&lt;/a&gt;&lt;br&gt;");
				$m+=$duration;
				if($m&gt;59)
					{
					$minsLeft = $m - 60;
					// add to array
					$minsLeftArray[$counter] = $minsLeft;
					$minsLeft="Y";
					$slotStatus="";
					}
			}
			elseif($bookedStatus=="BOOKED")
						{
						
						$unixSlotTimeLength = gmdate("i",$unixSlotTimeLength+60);
						if($unixSlotTimeLength=="00")
							{
							$unixSlotTimeLength = 59;
							}
                                                     echo("BOOKED&lt;br&gt;");
                                                     //$m+=$duration; //
				// ************** ERROR PART **************			
						
						$m=$unixSlotTimeLength;
						
				// *************************************** 		
						//echo("End Slot Time Min : $unixSlotTimeLength&lt;br&gt;");
						
						//$m=$unixSlotTimeLength;;
						//echo("minutes After: $m&lt;br&gt;");
						if($m&gt;59)
							{
							
							$minsLeft = $m - 60;
							$minsLeftArray[$counter] = $minsLeft;
							$minsLeft="Y";
							$slotStatus="";
							}
						}
			else
				{
				echo("NOT AVAILABLE&lt;br&gt;");
				$m+=$duration;
						
						if($m&gt;59)
							{
							
							$minsLeft = $m - 60;
							$minsLeftArray[$counter] = $minsLeft;
							$minsLeft="Y";
							$slotStatus="";
							
							}
							
						}
			
	
			// end of minute loop
			
			$m=$m-1;
			}// end of minute loop

FULL CODE :

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=“Content-Type” content=“text/html; charset=ISO-8859-1” />
<title>Untitled Document</title>
</head>

<body>
<p>
<?

if($createAppointment!=“”)
{
$query = “UPDATE tblTimeSlots SET slotStatus=‘BOOKED’ WHERE slotTime>=‘$slotFromTime’ AND slotTime<=$slotToTime AND clientID=‘$clientID’ AND staffID=‘$staffID’”;
echo (“$query<br>”);
mysql_query($query,$db);
}

//$todayStartDate = time();

//variables

$clientID = 123;
$staffID = 456;
$query = “SELECT timeSlotID, slotStartTime, slotFinishTime FROM tblTimeSlotDetails WHERE clientID=‘$clientID’ AND staffID=‘$staffID’”;
echo $query;
$result = mysql_query($query,$db);
$row = mysql_fetch_array($result);

$timeSlotID = $row[timeSlotID];
$hourStart = $row[slotStartTime];
$hourFinish = $row[slotFinishTime];

//$hourStart = 9;
//$hourFinish = 17;

if($duration==“”)
{
$duration = 25;
}
$slotDuration = $duration;
$slotsPerHour = 60/$slotDuration;

echo(“Duration : $duration”);

if($nextWeek!=“”)
{
$date = strtotime(“$day-$month-$year GMT”);
$todayNow = $date + 604800;
echo $todayNow;
}
else
{
$todayNow = time();
}

$dateDay = date(‘D’, $todayNow) ;
$dateNum = date(‘j’, $todayNow) ;
$dateYear = date(‘Y’, $todayNow);
$dateMonth = date(‘n’, $todayNow);

?>
</p>
<p><a href=“/test5c.php?duration=25”>Gel</a> = 25 min<br />
<a href=“/test5c.php?duration=15”>Soak</a> = 15min<br />
<a href=“/test5c.php?duration=45”>Nail</a> = 45min
<br /><a href=“/test5c.php?duration=10”>Scrub</a> = 10 min <br />
</p>
<p> </p>
<?
echo(“<a href=‘/test5b.php?nextWeek=Y&day=$dateNum&month=$dateMonth&year=$dateYear’>Next Week</a>”);?>
<table width=“900” border=“1” cellpadding=“0” cellspacing=“0”>
<tr><td>time</td>
<?
// create top row of days for current timr
for($i =0; $i<7; $i++)
{

$time = 86400 * $i;
$newTime = $todayNow + $time;	

$dateDay = date('D', $newTime) ;
$dateNum = date('j', $newTime) ;
echo("&lt;td&gt;$dateDay $dateNum&lt;/td&gt;");

}

?>
</tr>

<?
//$todayNow = time();

// hourly time slot rows + needs 7 vertical columns for time
for($h=$hourStart; $h<=$hourFinish; $h++)
{
echo(“<tr>”);

	// columns 7 for each day
	for($c = 0; $c &lt;7; $c++)
		{
		$counter++;
		if($h==$hourStart)
			{
			$minsLeft="";
			}
		
		$time = 86400 * $c;
		$newTime = $todayNow + $time;
		$dateNum = date('j', $newTime);
		
		$dateYear = date('Y', $newTime);
		$dateMonth = date('M', $newTime);
		
		// build up date and time from $h
		$timeStamp = "$dateNum $dateMonth $dateYear $h:00 GMT";
		$unix = strtotime($timeStamp);
		
		// left hand column
				if($c==0)
					{
					// display hour column on left
					if($h&gt;12 && $h&lt;=23)
						{
						$columnTime = $h - 12;
						$columnTime = $columnTime ."pm";
						}
					else
						{
						$columnTime = $h."am";
						}
					
					echo("&lt;td&gt;$columnTime&lt;/td&gt;");
					}
					// end of column on left
			         echo("&lt;td&gt;");
					
					 // START OF BOOKING TESTHOUR INTERGRATION //
	 if($minsLeft=="Y")
		{
		// get value from array
		$mins = $minsLeftArray[$counter-7];
		$minsLeft ="";
		}
	else
		{
		$mins=0;
		}
		
	for($m=$mins; $m&lt;60; $m++)
		{
		//echo("Minutes:$m&lt;br&gt;");
		$minutes = $m;
		$timeStamp = "$dateNum $dateMonth $dateYear $h:$minutes GMT";
		$unix = strtotime($timeStamp);
		$unixSlotTimeLength = $unix+(60*$duration)-60;
		
		$bookedStatus="";
		$availableStatus  = "";
		
		
		//check there is no booking
		$query = "SELECT slotStatus FROM tblTimeSlots WHERE slotTime&gt;='$unix' AND slotTime&lt;='$unixSlotTimeLength' AND timeSlotID=1 ORDER BY id ASC";
		//echo ("$query&lt;br&gt;");
		$result = mysql_query($query,$db);
		while($row = mysql_fetch_array($result))
			{
			$status = $row[slotStatus];
			
				if($status=="BOOKED")
					{
					$bookedStatus ="BOOKED";
					}
				elseif($status=="AVAILABLE")
					{
					$availableStatus = "AVAILABLE";
					}
				else
					{
					$availableStatus = "NOT AVAILABLE";
					}
				}
				
		
		
		if($availableStatus=="AVAILABLE" && $bookedStatus!="BOOKED")
			{
			 $timeStamp = "$dateNum $dateMonth $dateYear $h:$minutes GMT";
			
					if($minutes==0)
						{
						$minutes="00";
						}
				$showTime = gmdate("g:i",$unix);
				echo("&lt;a href='/test5c.php?createAppointment=Y&slotFromTime=$unix&slotToTime=$unixSlotTimeLength&clientID=$clientID&staffID=$staffID&timeSlotID=$timeSlotID'&gt;$showTime&lt;/a&gt;&lt;br&gt;");
				$m+=$duration;
				if($m&gt;59)
					{
					$minsLeft = $m - 60;
					// add to array
					$minsLeftArray[$counter] = $minsLeft;
					$minsLeft="Y";
					$slotStatus="";
					}
			}
			elseif($bookedStatus=="BOOKED")
						{
						
						$unixSlotTimeLength = gmdate("i",$unixSlotTimeLength+60);
						if($unixSlotTimeLength=="00")
							{
							$unixSlotTimeLength = 59;
							}
							
						echo("BOOKED&lt;br&gt;");
						//$m=$unixSlotTimeLength;
						$m+=$duration;
						
						//echo("End Slot Time Min : $unixSlotTimeLength&lt;br&gt;");
						
						//$m=$unixSlotTimeLength;;
						//echo("minutes After: $m&lt;br&gt;");
						if($m&gt;59)
							{
							
							$minsLeft = $m - 60;
							$minsLeftArray[$counter] = $minsLeft;
							$minsLeft="Y";
							$slotStatus="";
							}
						}
			else
				{
				echo("NOT AVAILABLE&lt;br&gt;");
				$m+=$duration;
						
						if($m&gt;59)
							{
							
							$minsLeft = $m - 60;
							$minsLeftArray[$counter] = $minsLeft;
							$minsLeft="Y";
							$slotStatus="";
							
							}
							
						}
			
	
			// end of minute loop
			
			$m=$m-1;
			}// end of minute loop
					
					
					
		
		echo("&lt;/td&gt;");
		} // end of column loop
echo("&lt;/tr&gt;");
} // end of hour loop
?&gt;

</table>

<p> </p>
<p> </p>
<table width=“400” border=“1” cellpadding=“0” cellspacing=“0”>
<tr>
<td width=“102” rowspan=“2”>1</td>
<td width=“69”>2</td>
<td width=“221” rowspan=“2”>4</td>
</tr>
<tr>
<td>3</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
<td>7</td>
</tr>
</table>
<p> </p>

I dont understand how you can have a booking from 9h00 til 9h50 when your system is working on 15mn slots ? surely that slot should end at either 9h45 or 10h00 ??

If your slots are infinately variable then you should be designing the system to work on individual minute slots !!

Normally there is a min slot time by which the company will charge ie 5 mins or 15mins or 30mins so that would be the basis of your calculations.

There are different slot times depending on service is 10 min slot, 15 min 45min etc, thats part of my problem it is counting up in the slot time interval. I need it to reset the timer when a booking is found so there is a time gap ie
slot interval 15 mins, counter 0, 15, 30, 45, 60
we have a booking that ends at say 9:25, at the moment next availble is 9:30. I need to change the minute loop value back to 25 and not 30.
If that makes sense!!

That makes alot of sense thanks mandes, what would be the best way to show time slots at say 15 min intervals on a 60 minute loop
would it be if($m %15)
{
show time
}

no sorry perhaps Im being thick here but I dont follow. (im talking about your No3 post here, youve since posted again whilst I was thinking :slight_smile: )

You have an appontment 9h00 to 9h25, thats fine but your counter needs to be set to the slot time of the next appointment being made not the finish time of the last appointment … no ?

You have in your database a list of appointments start and finish times ordered by date and time

You set your slot time for the appointment wishing to be made

you then scan the database looking at the finish time of app_A and start time of app_A+1, once you find a gap that is greater than the time your looking for you can offer that appointment.

A small function to work out the mins between different timestamps retrieved from your DB

function get_time_difference($start, $finish){ 
     return ceil(abs((strtotime($finish) - strtotime($start)) / 60)); 
}

thanks mandes going to scrap script. How would I show the duration times ie 15 min 30 min etc

Didn’t the discussion in this thread solve the problem?

It looks very similar to this one.