简体   繁体   中英

How to find time difference using php from html input form

I have three pages, the first page (html) request two date input from users to get the date. the second page (php) will show results that met the criteria and a select button for users to select, the third page (php) will display information selected by users from the second page. Right now I can pass the information from SQL to the third page but I could not grab the data information on the first page to the last page to show a day difference so I can calculate the total costs by multiplying rate * date difference.

第 1 页,html

第 2 页 搜索结果 第 3 页 从第二页选择结果

<div class="row">
<div class="col-sm-6">
<div class="form-group">
<span class="form-label">Drop off</span>
<input class="form-section" type="date" name="date1" required>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<span class="form-label">Pick up</span>
<input class="form-section" type="date" name="date2" required>
</div>
</div>
</div>
<div class="row">
              <?php
              ini_set('display_errors', 1);
              ini_set('display_startup_errors', 1);
              error_reporting(E_ALL);

              $severname = "localhost";
              $username = "root";
              $password = "";
              $dbname = "dbn";

                //$conn = mysqli_connect('xx', 'xx', 'xx','xx');
              $conn = mysqli_connect('xx', 'xx', 'xx','xx');
              //check connection
              if (mysqli_connect_errno())
              {echo nl2br("Failed to connect to MySQL: ". mysqli_connect_error() . "\n"); }
              else
              { echo nl2br("");}

              $city = mysqli_real_escape_string($conn, $_POST['city']);
              $state = mysqli_real_escape_string($conn, $_POST['state']);
              //$date1 = mysqli_real_escape_string($conn, $_POST['date1']);
              //$date2 = mysqli_real_escape_string($conn,$_POST['date2']);
              $pet = mysqli_real_escape_string($conn, $_POST['pet']);


              //var_dump($date2);

              $sql = "SELECT CONCAT(s.lname, ' ', s.fname) AS fullName, s.pic, s.ratings, s.rate, s.phone, s.address FROM Pet_Sitter AS s WHERE s.city='".$city."' AND s.state='".$state."' AND s.pet_type='".$pet."' "
                  ;
              //echo $sql."<br/>";
              $result = mysqli_query($conn,$sql);
              if (!$result) {
                  printf("Error: %s\n", mysqli_error($conn));
                  exit();
              }

              if ($result=mysqli_query($conn,$sql))
                {
                // Return the number of rows in result set
                $rowcount=mysqli_num_rows($result);
                printf("");
                }

              //var_dump($result);

              $num_rows = mysqli_num_rows($result);
              ?> <div class="container"> <?php


              if ($result->num_rows > 0) {
                while ($row = $result->fetch_assoc()) {
                    $rate = $row['rate'];
                    $fullName = $row['fullName'];

                //var_dump($row);
                // echo "<tr><td>" . $row['fullName'] . " </td><td>" . $row['ratings'] . " </td><td>" . $row['rate'] . " </td></tr>";
                // echo '<div class="listing-item">';
                echo '<div class="listing-item">';
                echo '<article class="geodir-category-listing fl-wrap">';
                echo '<div class="geodir-category-img">';
                echo '<img src="../images/all/1.jpg" alt="">';
                echo '<div class="overlay"></div>';
                echo '<div class="list-post-counter"><span>' . '$'. $row['rate'] . '</span></div>';
                echo '</div>';
                echo '<div class="geodir-category-content fl-wrap">';
                echo '<a class="listing-geodir-category" href="payment.php?rate='.$rate.'&fullname='.$fullName.'">Select</a>';
                echo '<h3><a href="../index.html">' . $row['fullName'] . '</a></h3>';
                echo '<p>PET-SITTER INFORMATION</p>';
                echo '<div class="geodir-category-options fl-wrap">';
                echo '<span>' . 'Reviews ' . $row['ratings'] .'</span>';
                echo '<div class="geodir-category-location">'. $row['address'] . " · " . $row['phone'] .
                '</div>';
                echo '</div>';
                echo '</div>';
                echo '</article>';
                echo '</div>';
              }?>
              <?php



              } else {
                  echo "0 results";
                }
                  mysqli_close($conn);

              ?>
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->

  <title>Work</title>

  <!-- Bootstrap -->
  <link type="text/css" rel="stylesheet" href="css/bootstrap-grid.min.css" />
  <link type="text/css" rel="stylesheet" href="css/style.css" />
  <link rel="shortcut icon" href="images/pet-logo.png">
</head>


<body>
  <!-- Menu Bar -->
  <div class="menu">
    <div class="container-fluid my-container">
      <div class="row my-row">
        <div class="col-1">
          <div class="menu-logo">
            <a href="index.html"><img src="images/pet-logo.png" alt=""></a>
          </div>
        </div>
        <div class="nav-col col-7"></div>
        <div class="nav-col col-2">
          <div class="dropdown">
            <button class="dropbtn"><a href="index.html">Home</a></button>
            <div class="dropdown-content">
              <a href="about.html">About Us</a>
              <a href="contacts.html">Contact</a>
            </div>
          </div>
        </div>
        <div class="nav-col col-1"><a href="login.html">Login</a></div>
        <div class="nav-col col-1"><a href="register.html">Sign Up</a></div>
      </div>
    </div>
  </div>
  <!-- End of Menu -->

  <div id="booking" class="section">
    <div class="section-center">
      <div class="container">
        <div class="row">
          <div class="col-4">
             <div class="booking-form">
                <form method="POST">
                <h1>Confirm Order</H1>
                    <div class="booking-form">
                        <span class="form-label"><?php
    $fullName = isset($_GET['fullname']) ? $_GET['fullname']: '';
    echo 'Sitter: <br>' . $fullName;
?></span>
                        </br>
                        </br>
                            <div class="form-group">
                                <span class="form-label"><?php
    $rate = isset($_GET['rate'])? $_GET['rate']:'';
    echo 'Total Amount: <br>$'. $rate;


?></span>


                                    </div>
                                </div>
                                <br>
                                <br>
                            <div class="form-btn">
                        <a class="submit-btn" href = "https://www.tutorialspoint.com/php/php_sessions.htm">Confirm and Pay</a>
                    </div>
                </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>



</body>

</html>

In order to send your data you want to put them in a form, and give it an action that points to the endpoint you want to handle the datas at.

<form action="/the/route/to/your/phpfile.php" method="post">
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<span class="form-label">Drop off</span>
<input class="form-section" type="date" name="date1" required>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<span class="form-label">Pick up</span>
<input class="form-section" type="date" name="date2" required>
</div>
</div>
</div>
<div class="row">
<button type="submit">Click here</button>
</form>

After that you at the destination you can get what you need by retrieving data from $_POST array, like you did use it already. You can alternatively have a submit button, but if I'm right you can also submit by pressing enter.

Btw: If you want to calculate a lot with dates, you might want to implement Carbon in your project. https://carbon.nesbot.com/docs/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM