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 <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>12 && $h<=23)
{
$columnTime = $h - 12;
$columnTime = $columnTime ."pm";
}
else
{
$columnTime = $h."am";
}
echo("<th width='45'><span class='hourtext'>$columnTime</span></th>");
}
// end of column on left
echo("<td width='70' valign='top'>");
// 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<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>='$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>='$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";
}
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>=time())
{
if($showTimeHours>9)
{
echo("<a href='/serviceWidgetBook.php?createAppointment=Y&slotFromTime=$unix&slotToTime=$unixSlotTimeLength&clientID=$clientID&staffID=$staffID&timeSlotID=$timeSlotID&serviceID=$serviceID&locationID=$locationID' class='available'>$showTime</a>");
}
else
{
echo("<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'> $showTime </a></class>");
}
}
if($loop<$slotNumber)
{
echo("<br />");
}
else
{
}
$m++;
if($m>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("<span class='booked'> </class>");
if($loop<$slotNumber)
{
echo("<br />");
}
else
{
}
$m++;
if($m>59)
{
$minsLeft = $m - 60;
$minsLeftArray[$counter] = $minsLeft;
$minsLeft="Y";
$slotStatus="";
}
}
else
{
// not available
if($loop<$slotNumber)
{
echo("<span class='notavailable'>oooooooo</class><br />");
}
else
{
echo("<span class='notavailable'>oooooooo</class>");
}
$m++;
if($m>59)
{
$minsLeft = $m - 60;
$minsLeftArray[$counter] = $minsLeft;
$minsLeft="Y";
$slotStatus="";
}
}
// end of minute loop
$m=$m-1;
}// end of minute loop
echo("</td>");
} // end of column loop
echo("</tr>");
} // end of hour loop
?>
</table>