How to Use Date in Search using PHP

This tutorial will help you on how to use Date in Search using PHP. If you are looking for this kind of function then you are at the right place. The user learns how to search data in their table using the date. Create Table
  1. Kindly open your PHPMyAdmin in your web browser.
  2. Create database name that you want to name it.
  3. After that, click the SQL button then copy and paste the SQL source code below to create a table in your database.
  1. CREATE TABLE `tbl_member` (
  2. `tbl_member_id` INT(11) NOT NULL,
  3. `tbl_member_name` VARCHAR(100) NOT NULL,
  4. `tbl_member_contact` VARCHAR(100) NOT NULL,
  5. `tbl_member_added` datetime NOT NULL
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Create simple Markup We are going to create our table to display all the data from the database and the form field where you can found the input element of date to use for the searching data. Look the source codes below and study. This source code use for the searching data from the database using a date.
  1. <form method="post" action="search.php">
  2. <div>
  3. <label>Search using Date</label>
  4. <input type="date" name="date_from" value="<?php echo date('Y-m-d'); ?>" />
  5. </div>
  6. <button type="submit" name="search">Search</button>
  7. </form>
This is our table that contains all the data from the database.
  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 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

This is the GUI for "index.php". Result All source code above saved it as your "index.php". After compiling the source code for the index.php, we are going to create for the "search.php" and to construct the query for the search function using a date. This is the form field after the users choose a specific date to search data in the table. Take a look the 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']: ''; ?>" />
  5. </div>
  6. <button type="submit" name="search">Search</button>
  7. </form>
This PHP source code query helps us to search the data using date given by the user. Put this PHP source code in the HTML table.
  1. <?php
  2. include ('database.php');
  3.  
  4. $result = $database->prepare("SELECT * FROM tbl_member where (tbl_member.tbl_member_added BETWEEN '" . $_POST['date_from'] . " 00:00:01' and '" . $_POST['date_from'] . " 23:59:59') order by tbl_member_id DESC");
  5. $result->execute();
  6.  
  7. for ($count = 0; $row_member = $result->fetch(); $count++)
  8. {
  9. $id = $row_member['tbl_member_id'];
  10. ?>
  11. <?php
  12. } ?>
This is the full source code for the searching data using a date on the table.
  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_from']." 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

The GUI for "search.php". Result That's all, compile all the source code above then try it to your computer and open your web browser to run this simple tutorial. Download the full source code below. Thank you.

Comments

Add new comment