PHP - Search Data Between Two Dates

In this tutorial we will create a Search Data Between Two Dates using PHP. This code can search a data base on the dates when the user entered the date and click the search button. The code itself use MySQLi SELECT to initiate a row fetch and by adding a parameter BETWEEN it can search a specific data by providing to valid dates in the form input. This is a user-friendly kind of program feel free to modify it and use it as your own. We will be using PHP as a scripting language that manage a database server to handle a bulk of data per transaction. It describe as an advance technology that manage both server and control-block of your machine. .

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_date, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_date");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Search Data Between Two Dates</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-success" type="button" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add product</button>
  18. <br /><br />
  19. <form method="POST" action="">
  20. <div class="form-inline">
  21. <input type="date" class="form-control" name="from" required="required"/>
  22. <input type="date" class="form-control" name="to" required="required"/>
  23. <button class="btn btn-primary" name="search"><span class="glyphicon glyphicon-search"></span> Search</button>
  24. </div>
  25. </form>
  26. <br />
  27. <table class="table table-bordered">
  28. <thead class="alert-info">
  29. <tr>
  30. <th>Product name</th>
  31. <th>Brand</th>
  32. <th>Price</th>
  33. <th>Date Receive</th>
  34. </tr>
  35. </thead>
  36. <tbody><?php include 'search.php'?></tbody>
  37. </table>
  38. </div>
  39. <div class="modal fade" id="form_modal" aria-hidden="true">
  40. <div class="modal-dialog">
  41. <div class="modal-content">
  42. <form method="POST" action="save.php">
  43. <div class="modal-header">
  44. <h3 class="modal-title">Add Product</h3>
  45. </div>
  46. <div class="modal-body">
  47. <div class="col-md-2"></div>
  48. <div class="col-md-8">
  49. <div class="form-group">
  50. <label>Product name</label>
  51. <input type="text" class="form-control" name="product_name" required="required"/>
  52. </div>
  53. <div class="form-group">
  54. <label>Brand</label>
  55. <input type="text" class="form-control" name="brand" required="required"/>
  56. </div>
  57. <div class="form-group">
  58. <label>Price</label>
  59. <input type="number" min="0" class="form-control" name="price" required="required"/>
  60. </div>
  61. <div class="form-group">
  62. <label>Date Receive</label>
  63. <input type="date" class="form-control" name="date_receive" required="required"/>
  64. </div>
  65. </div>
  66. </div>
  67. <br style="clear:both;"/>
  68. <div class="modal-footer">
  69. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  70. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  71. </div>
  72. </form>
  73. </div>
  74. </div>
  75. </div>
  76. <script src="js/jquery-3.2.1.min.js"></script>
  77. <script src="js/bootstrap.js"></script>
  78. </body>
  79. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the data inputs to the database server. To do this just copy and write these block of codes below inside the text editor, then save it as save.php.
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['save'])){
  5. $product_name = $_POST['product_name'];
  6. $brand = $_POST['brand'];
  7. $price = $_POST['price'];
  8. $date = $_POST['date_receive'];
  9.  
  10. mysqli_query($conn, "INSERT INTO `product` VALUES('', '$product_name', '$brand', '$price', '$date')") or die(mysqli_error());
  11. header("location: index.php");
  12. }
  13. ?>

Creating the Main Function

This code contains the main function of the application. This code will search a specific data base on dates when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as search.php.
  1. <?php
  2. require 'conn.php';
  3.  
  4. if(ISSET($_POST['search'])){
  5. $from = $_POST['from'];
  6. $to = $_POST['to'];
  7. $query = mysqli_query($conn, "SELECT * FROM `product` WHERE date_receive BETWEEN '$from' AND '$to'") or die(mysqli_error());
  8. while($fetch = mysqli_fetch_array($query)){
  9. echo "<tr>
  10. <td>".$fetch['product_name']."</td>
  11. <td>".$fetch['brand']."</td>
  12. <td>".$fetch['price']."</td>
  13. <td>".$fetch['date_receive']."</td>
  14. </tr>";
  15.  
  16. }
  17. }else{
  18. $query = mysqli_query($conn, "SELECT * FROM `product`") or die(mysqli_error());
  19. while($fetch = mysqli_fetch_array($query)){
  20. echo "<tr>
  21. <td>".$fetch['product_name']."</td>
  22. <td>".$fetch['brand']."</td>
  23. <td>".$fetch['price']."</td>
  24. <td>".$fetch['date_receive']."</td>
  25. </tr>";
  26.  
  27. }
  28. }
  29. ?>
There you have it we successfully created Search Data Between Two Dates using jQuery. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Comments

Submitted byjoe manchion (not verified)on Wed, 03/17/2021 - 07:36

I realize that this is an old post, but you put the wrong database in the zip file

Add new comment