How to Use Two Dates in Search

If you are looking for on how to Use Two Dates in Search then you are at the right place. After creating the tutorial of Date in Search using PHP, we are going to use two dates for searching data. The same database table we are going to use for this tutorial. Follow the steps in the tutorial of Date in Search using PHP. All we have to do is to upgrade using a date to search in the data table, we are going to use two dates to search data from the database instead of one date. Related Code: Date in Search using PHP Example source codes for this tutorial. This source code contains the form field where the two dates used for the user to search data in the table. Check the source code below.
  1. <form method="post" action="search.php">
  2. <div>
  3. <label>Date From</label>
  4. <input type="date" name="date_from" value="<?php echo (isset ($_POST['date_from'])) ? $_POST['date_from']: ''; ?>" class="form-control" />
  5. </div>
  6. <div>
  7. <label>Date To</label>
  8. <input type="date" name="date_to" value="<?php echo (isset ($_POST['date_to'])) ? $_POST['date_to']: ''; ?>" class="form-control" />
  9. </div>
  10. <button type="submit" name="search">Search</button>
  11. </form>
Also, we have sample source code for displaying data from the database where the user search using two dates on the web page. Take a look the source code on how to construct the query using two dates.
  1. <tr>
  2. <th>Member Name</th>
  3. <th>Contact</th>
  4. <th>Date Added</th>
  5. </tr>
  6. <?php
  7. include ('database.php');
  8. $result = $database->prepare ("SELECT * FROM tbl_member where (tbl_member.tbl_member_added BETWEEN '".$_POST['date_from']." 00:00:01' and '".$_POST['date_to']." 23:59:59') order by tbl_member_id DESC");
  9. $result ->execute();
  10. for ($count=0; $row_member = $result ->fetch(); $count++){
  11. $id = $row_member['tbl_member_id'];
  12. ?>
  13. <tr>
  14. <td><?php echo $row_member['tbl_member_name']; ?></td>
  15. <td><?php echo $row_member['tbl_member_contact']; ?></td>
  16. <td><?php echo date("M d, Y h:i:s A", strtotime ($row_member['tbl_member_added'])); ?></td>
  17. </tr>
  18. <?php } ?>

Output

Check the result below. ResultResultRelated Code: Date in Search using PHP Hope you will find useful this tutorial. Enjoy coding. Thank you.

Add new comment