SitePoint Sponsor

User Tag List

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

    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<br>");

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

    for($m=$mins; $m<60; $m++)
    {
    //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-1;
    //echo("Minute:$m<br>");
    if($m>59)
    {
    $minsLeft = $m - 60;
    }
    }
    else
    {
    echo("$id :BOOKED $timeStamp<br>");
    $m+=$duration-1;


    }





    }
    }

  2. #2
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You need to add the duration after the test of minute if($m>59), so update your code to something like this:
    PHP Code:
    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

  3. #3
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Please disregard my post above. I wasn’t thinking right.

  4. #4
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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?

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Still doing way too much variable storing, and farrrrr too many queries.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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;
               }
           }
       }
    ?>

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


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
  •