SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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>='$unix' AND slotTime<='$unixSlotTimeLength' AND timeSlotID=1 ORDER BY id ASC";
    //echo ("$query<br>");
    $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("<a href='/test5c.php?createAppointment=Y&slotFromTime=$unix&slotToTime=$unixSlotTimeLength&clientID=$clientID&staffID=$staffID&timeSlotID=$timeSlotID'>$showTime</a><br>");
    $m+=$duration;
    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;
    }
    echo("BOOKED<br>");
    //$m+=$duration; //
    // ************** ERROR PART **************

    $m=$unixSlotTimeLength;

    // ***************************************
    //echo("End Slot Time Min : $unixSlotTimeLength<br>");

    //$m=$unixSlotTimeLength;;
    //echo("minutes After: $m<br>");
    if($m>59)
    {

    $minsLeft = $m - 60;
    $minsLeftArray[$counter] = $minsLeft;
    $minsLeft="Y";
    $slotStatus="";
    }
    }
    else
    {
    echo("NOT AVAILABLE<br>");
    $m+=$duration;

    if($m>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>&nbsp;</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("<td>$dateDay $dateNum</td>");

    }
    ?>
    </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 <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("<td>$columnTime</td>");
    }
    // end of column on left
    echo("<td>");

    // START OF BOOKING TESTHOUR INTERGRATION //
    if($minsLeft=="Y")
    {
    // get value from array
    $mins = $minsLeftArray[$counter-7];
    $minsLeft ="";
    }
    else
    {
    $mins=0;
    }

    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 no booking
    $query = "SELECT slotStatus FROM tblTimeSlots WHERE slotTime>='$unix' AND slotTime<='$unixSlotTimeLength' AND timeSlotID=1 ORDER BY id ASC";
    //echo ("$query<br>");
    $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("<a href='/test5c.php?createAppointment=Y&slotFromTime=$unix&slotToTime=$unixSlotTimeLength&clientID=$clientID&staffID=$staffID&timeSlotID=$timeSlotID'>$showTime</a><br>");
    $m+=$duration;
    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;
    }

    echo("BOOKED<br>");
    //$m=$unixSlotTimeLength;
    $m+=$duration;

    //echo("End Slot Time Min : $unixSlotTimeLength<br>");

    //$m=$unixSlotTimeLength;;
    //echo("minutes After: $m<br>");
    if($m>59)
    {

    $minsLeft = $m - 60;
    $minsLeftArray[$counter] = $minsLeft;
    $minsLeft="Y";
    $slotStatus="";
    }
    }
    else
    {
    echo("NOT AVAILABLE<br>");
    $m+=$duration;

    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>








    <p>&nbsp;</p>
    <p>&nbsp;</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>&nbsp;</p>

  2. #2
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!!

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    }

  5. #5
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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 :-) )

    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 Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  6. #6
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    A small function to work out the mins between different timestamps retrieved from your DB

    PHP Code:
    function get_time_difference($start$finish){ 
         return 
    ceil(abs((strtotime($finish) - strtotime($start)) / 60)); 

    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Location
    Poole, Dorset, United Kingdom
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks mandes going to scrap script. How would I show the duration times ie 15 min 30 min etc

  8. #8
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Didn’t the discussion in this thread solve the problem?
    http://www.sitepoint.com/forums/show...utes-Past-Hour

    It looks very similar to this one.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •