Hourly Slot System With Loops Keep Losing Minutes Past Hour

Hi All
Im adding a slot system into our business and are having a few issues with the times. It works with two loops one for the hour and one for the minutes.
Each slot has a duration ie 45 mins the system starts the hour and minute count looks up in the database see if that time is availble and applies a time lot of 20 mins to it. The counted adds on the 45 mins and does the same thing again until it hits the 60 mins then jumps back to the hour loop and add on the times
ie 9:00 slot
9:45 slot
10: 30 slot
11:15 slot and so on.

Code works ok but times can be off by a minute or so when you pass through an hour. Any help would be great

Thanks Steve

Code…
$clientID = 123;
$hourStart = 9;
$hourFinish = 16;
$duration = 25;
$slotDuration = $duration;
$slotsPerHour = 60/$slotDuration;

for($h=$hourStart; $h<=$hourFinish; $h++)
{

	echo("Hour:$h&lt;br&gt;");
	
	if($minsLeft!="")
		{
		$mins = "$minsLeft";
		}
	else
		{
		$mins=0;
		}
		
	for($m=$mins; $m&lt;60; $m++)
		{
		//echo("Minute:$m&lt;br&gt;");
		$minutes = $m;
		$timeStamp = "25 Apr 2012 $h:$minutes GMT";
		$unix = strtotime($timeStamp);
		
		
		$query = "SELECT id, slotStatus FROM tblTimeSlots WHERE slotTime&gt;='$unix'";
		
		$result = mysql_query($query,$db);
		$row = mysql_fetch_array($result);
		$slotStatus = $row[slotStatus];
		$id = $row[id];
			
			if($slotStatus=="AVAILABLE")
			{
				$timeStamp = "25/4/2012 $h:$m GMT";
				echo("$id :AVAILABLE $timeStamp&lt;br&gt;");
				
				$m+=$duration-1;
				//echo("Minute:$m&lt;br&gt;");
				if($m&gt;59)
					{
					$minsLeft = $m - 60;
					}
			}
			else
						{
						echo("$id :BOOKED $timeStamp&lt;br&gt;");
						$m+=$duration-1;
						
						
						}
						
			
			
	
			
}
		}

You need to add the duration after the test of minute if($m>59), so update your code to something like this:

if($slotStatus=="AVAILABLE")
{
	$timeStamp = "25/4/2012 $h:$m GMT";
	echo("$id :AVAILABLE $timeStamp<br>");
	if($m>59)
	{
		$minsLeft = $m - 60;
	}
}
else
{
	echo("$id :BOOKED $timeStamp<br>");
}
$m+=$duration-1;

Please disregard my post above. I wasn’t thinking right.

After a decent night’s sleep, I think I could see better now. Here is the complete code for you to test it out:

<?php
   $clientID = 123;
   $hourStart = 9;
   $hourFinish = 16;
   $duration = 25;
   $slotDuration = $duration;
   $slotsPerHour = 60/$slotDuration;
   $slotStatus="AVAILABLE";

   for($h=$hourStart; $h<=$hourFinish; $h++)
   {
	   echo("<br/>Hour:$h<br/>");

	   if($minsLeft!="")
	   {
		   $mins = "$minsLeft";
	   }
	   else
	   {
		   $mins=0;
	   }

	   for($m=$mins; $m<60;)
	   {
		   //echo("Minute:$m<br>");
		   $minutes = $m;
		   $timeStamp = "25 Apr 2012 $h:$minutes GMT";
		   $unix = strtotime($timeStamp);
/*
		   $query = "SELECT id, slotStatus FROM tblTimeSlots WHERE slotTime>='$unix'";
		   $result = mysql_query($query,$db);
		   $row = mysql_fetch_array($result);
		   $slotStatus = $row[slotStatus];
		   $id = $row[id];
*/
		   if($slotStatus=="AVAILABLE")
		   {
			   $timeStamp = "25/4/2012 $h:$m GMT";
			   echo "$id :AVAILABLE $timeStamp<br/>";
			   $m+=$duration;
			   if($m>59)
			   {
				   $minsLeft = $m - 60;
			   }
		   }
		   else
		   {
			   $m+=$duration;
			   echo("$id :BOOKED $timeStamp<br/>");
		   }
	   }
   }
?>

Note that I have commented out the database query part.

Here is a question for you - Do you know why this fixes the problem?

Still doing way too much variable storing, and farrrrr too many queries.

Maybe so but right now he is looking for the solution to his first reported problem. I did a second look and found my script needs more changes. Hopefully is this the last one:

<?php
   $clientID = 123;
   $hourStart = 9;
   $hourFinish = 16;
   $duration = 25;
   $slotDuration = $duration;
   $slotsPerHour = 60/$slotDuration;
   //$slotStatus="AVAILABLE";

   for($h=$hourStart; $h<=$hourFinish; $h++)
   {
	   echo("<br/>Hour:$h<br/>");

	   if($minsLeft!="")
	   {
		   $mins = "$minsLeft";
	   }
	   else
	   {
		   $mins=0;
	   }

	   for($m=$mins; $m<60;)
	   {
		   //echo("Minute:$m<br>");
		   $minutes = $m;
		   $timeStamp = "25 Apr 2012 $h:$minutes GMT";
		   $unix = strtotime($timeStamp);
/*
		   $query = "SELECT id, slotStatus FROM tblTimeSlots WHERE slotTime>='$unix'";
		   $result = mysql_query($query,$db);
		   $row = mysql_fetch_array($result);
		   $slotStatus = $row[slotStatus];
		   $id = $row[id];
*/
		   if($slotStatus=="AVAILABLE")
		   {
			   $timeStamp = "25/4/2012 $h:$m GMT";
			   echo "$id :AVAILABLE $timeStamp<br/>";
		   }
		   else
		   {
			   echo("$id :BOOKED $timeStamp<br/>");
		   }
		   $m+=$duration;
		   if($m>59)
		   {
			   $minsLeft = $m - 60;
		   }
	   }
   }
?>

Hi Tom
Many Thanks for all your help I’m guessin keeping the $m+=$duration outside the if else part of loop so there is no need to duplicate the code and make it more efficient.
Again thanks for your help

Steve