需要循环在HTML中添加30分钟的时间

时间:2017-11-14 11:46:38

标签: php html

我使用html和php创建了一个允许用户预约约会的网页,但是我的插槽每30分钟一次,但我目前正在做的方式不会在数据库上添加30分钟

<?php ob_start( ); ?>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="Nav.css">

    <title>Book Barber</title></head>
    <body>
    <?php
            echo " <div class='navigation'><a href='BarberHomeScreen.php'>Home</a><div class='dropdown'><button class='dropdownButton'>Account<i class='fa fa-caret-down'></i></button><div class='dropdownContent'><a href='MyAccount.php'>My Account</a><a href='SignOut.php'>Sign Out</a></div></div></div>";
    ?>
        <h1>Booking a barber appointment for <?php session_start();echo $_SESSION['customerName'];?></h1>
        <form method="POST" action="#">

            <input type="date" name="selectDate" />
            <input type="submit" name="submitDate"/>  


        <?php
            if(isset($_POST['submitDate'])){

                //if the submit button has been pressed, connect to the db
                //and search for all the available tables on that selected day
                $conn = mysql_connect(localhost, "root", "");
                if(!$conn){
                    die("Could not connect: " . mysql_error());
                }

                  $selectedDb = mysql_select_db('booking', $conn);
                  if(!$selectedDb){
                    die("Can't use the selected db: " . mysql_error());
                  }

                  //selects all the bookings for the date chosen in the form
                  $query= "SELECT * FROM booking WHERE BookingDate = '" . $_POST['selectDate'] . "' ORDER BY customerID, BookingTime";
                  $result = mysql_query($query);
                  //stop php while table headers outputted
                  ?>

                  <table border="1">
                      <tr></tr>
                      <tr>
                          <th>Barber</th>
                          <th>9:00</th>
                          <th>9:30</th>
                          <th>10:00</th>
                          <th>10:30</th>
                          <th>11:00</th>
                          <th>11:30</th>
                          <th>12:00</th>
                          <th>12:30</th>
                          <th>13:00</th>
                          <th>13:30</th>
                          <th>14:00</th>
                          <th>14:30</th>
                          <th>15:00</th>
                          <th>15:30</th>
                          <th>16:00</th>
                      </tr>
                  <?php //start php again to output the bookings available or not

                 /*The next bulk of php is outputting the table showing which slots are booked and which are available.
                 two while loops are needed, the outer one loops through the tables, the inner while loops through
                 each of the times for the table. 
                 Then while the loops are repeating they check if this booking
                 is for the current timeslot and table being looked at. If so it puts an X in the td and carries out mysql_fetch_assoc
                 again to get the next booking from the $result. This continues for each of the slots in the table. 
                 */
                    $row = mysql_fetch_assoc($result);
                       $time = 9;
                       echo "<tr>";
                       echo "<td>" . $count . "</td>";
                       while($time <= 16){//time begins at 9 and stops at 16. Would be better to get this from the db too.
                           if((Time($row['BookingTime'])==$time)){
                               echo "<td style='background-color:lightCoral'>X</td>";
                               $row = mysql_fetch_assoc($result);
                           }else{
                               echo "<td style='background-color:lightGreen'><a href='MakeBarberBooking.php?&time=" . $time. "&date=" . $_POST['selectDate'] ."'>Book</a></td>";
                           }

                           $time=$time+0.5;
                       }
                        echo "</tr>";

                    }//end while


            //end if submit pressed

                ?>
                </table>
            </form>
    </body>
</html>
<?php 

ob_end_flush( );
 ?>            

系统使用time = time + 0.5循环每个时段,但是将时间记录为10.5而不是10:30,例如

2 个答案:

答案 0 :(得分:0)

您可以使用PHP的DatePeriod功能。

首先创建开始和结束时间。然后创建一个日期间隔并循环对象。

N.B。值得注意的是,您可能需要将间隔添加到dtEnds以使循环包含最后一个句点。

// set format the date string is formatted as 
$format = 'Y-m-d H:i:s';

// set the start and end date 
$dtStarts = new DateTime();
$dtEnds   = new DateTime::createFromFormat($format, '2017-12-01 06:00:00');

// create intervals that we would like to loop over
// i.e. 1 day at a time, or every 45 minutes
$dtInterval         = DateInterval::createFromDateString('1 days');
$dtIntraDayInterval = DateInterval::createFromDateString('45 minutes');

// set day period/range
$dpPeriod = new DatePeriod( $dtCalStarts, $dtInterval, $dtCalEnds );

// loop oveer each period in the day
foreach ( $dpPeriod as $dtDay ){
    echo "<pre>";
    var_dump($dtDay);
    echo "</pre>";
}

答案 1 :(得分:0)

除了ProEvilz已经评论过Sql Injection之外,添加.5不会神奇地将其变成日期/时间格式。

使用日期对象(或php5.4 +中的DateTime类)以及每次使用此迭代:

初始值为

$currentTime = "09:00";
$nextTime = date("H:i", strtotime($currentTime ." +30 MINUTE"));

$ nextTime将是09:30 ..