Daily Booking Calendar Script 7 Second Load Time - Help

Hi All
My booking calendar script is running at 7 seconds to load a 9 - 5 day which is way to long.
I’ve built it around 3 for loops… day, hour & minute which is slowing it down. Theres alot going on in this code and I’m no amazing programmer
any ideas where I can speed it up without totally re-writting it. The main part of the script is below

for($h=$hourStart; $h<=$hourFinish; $h++)
{
echo(“<tr align=‘center’>”);

	// 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;th width='45'&gt;&lt;span class='hourtext'&gt;$columnTime&lt;/span&gt;&lt;/th&gt;");
					}
					// end of column on left
			         echo("&lt;td width='70' valign='top'&gt;");
		
					 // START OF BOOKING TESTHOUR INTERGRATION //
	
	 if($minsLeft=="Y")
		{
		// get value from array
		$mins = $minsLeftArray[$counter-7];
		$minsLeft ="";
		}
	else
		{
		$mins=0;
		}
	$loop=0;
	// minutes part of system	
	for($m=$mins; $m&lt;60; $m+=$timeInterval)
		{
	
		$loop++;
		$minutes = $m;
		$timeStamp = "$dateNum $dateMonth $dateYear $h:$minutes GMT";
		$unix = strtotime($timeStamp);
		
		$unixSlotTimeLength = $unix+(60*$duration)-60;
		
		$bookedStatus="";
		$availableStatus  = "";
		$NotAvailableStatus ="";
		


		// check end of day booking if runs over to slot duration
		$query = "SELECT slotStatus FROM tblTimeSlots WHERE slotTime&gt;='$unix' AND slotTime='$unixSlotTimeLength' AND timeSlotID='$timeSlotID' ORDER BY id ASC";
		$result = mysql_query($query,$db);
		$total = mysql_num_rows($result);
		if(!$total)
				{
				$NotAvailableStatus = "NOT AVAILABLE";
				}
		
		//check there is no booking
		$query = "SELECT slotStatus FROM tblTimeSlots WHERE slotTime&gt;='$unix' AND slotTime&lt;='$unixSlotTimeLength' AND timeSlotID='$timeSlotID' ORDER BY id ASC";
		$result = mysql_query($query,$db);
		$total = mysql_num_rows($result);
		if(!$total)
				{
				$NotAvailableStatus = "NOT AVAILABLE";
				}


	
		
		while($row = mysql_fetch_array($result))
			{
			$status = $row[slotStatus];
			
				if($status=="BOOKED")
					{
					$bookedStatus ="BOOKED";
					}
				elseif($status=="AVAILABLE")
					{
					$availableStatus = "AVAILABLE";
					}
				elseif($status=="NOT AVAILABLE")
					{
					$NotAvailableStatus = "NOT AVAILABLE";
					}
				else
					{
					
					}
				}
				


		
		
		if($availableStatus=="AVAILABLE" && $bookedStatus!="BOOKED" && $NotAvailableStatus!="NOT AVAILABLE")
			{
			 $timeStamp = "$dateNum $dateMonth $dateYear $h:$minutes GMT";
			
					if($minutes==0)
						{
						$minutes="00";
						}
				$showTime = gmdate("g:ia",$unix);
				$showTimeHours = gmdate("g",$unix);
					
					
					if($unix&gt;=time())
						{
					
					if($showTimeHours&gt;9)
						{
				echo("&lt;a href='/serviceWidgetBook.php?createAppointment=Y&slotFromTime=$unix&slotToTime=$unixSlotTimeLength&clientID=$clientID&staffID=$staffID&timeSlotID=$timeSlotID&serviceID=$serviceID&locationID=$locationID' class='available'&gt;$showTime&lt;/a&gt;");	
						}
					else
						{
					echo("&lt;a href='/serviceWidgetBook.php?createAppointment=Y&slotFromTime=$unix&slotToTime=$unixSlotTimeLength&clientID=$clientID&staffID=$staffID&timeSlotID=$timeSlotID&serviceID=$serviceID&locationID=$locationID' span class='available' align='center'&gt;&nbsp;$showTime&nbsp;&lt;/a&gt;&lt;/class&gt;");			
						}
						}
					
					if($loop&lt;$slotNumber)
					{
				echo("&lt;br /&gt;");
					}
				else
					{
					
					}
				
				$m++;
				
				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;
							}
						// booked	
					echo("&lt;span class='booked'&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/class&gt;");
							
							if($loop&lt;$slotNumber)
							{
						echo("&lt;br /&gt;");
							}
						else
							{
							
							}
					
						$m++;
						
						
						if($m&gt;59)
							{
							
							$minsLeft = $m - 60;
							$minsLeftArray[$counter] = $minsLeft;
							$minsLeft="Y";
							$slotStatus="";
							}
						}
			else
				{
				// not available
				if($loop&lt;$slotNumber)
					{
				echo("&lt;span class='notavailable'&gt;oooooooo&lt;/class&gt;&lt;br /&gt;");
					}
				else
					{
					echo("&lt;span class='notavailable'&gt;oooooooo&lt;/class&gt;");
					}
				$m++;
				
						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>

For each hour, day, and minute interval you are hitting your database… twice. Now think about that carefully, how many times are you hitting your database? 9-5 is 8, 7 days, let’s assume your $timeInterval is set at 15 minutes, so that means 4 iterations per hour. So that is 8 * 7 * 4 which equals 224 * 2 (two database calls) queries, which means you are hitting it 448 times!

That can’t be good. So what you need to do, is return the results up front in 1 or 2 calls, then filter your results based on the hour, day, and interval so you know you can display it.

Thanks for that it gives me something to work with had a duplicate mysql query which I hadn’t spotted.
Think put into an array and pull out when needed would help

Thanks Steve