PHP - Search Data Between Two Dates

Submitted by: 
Language: 
Visitors have accessed this post 291 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Search Data Between Two Dates</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <button class="btn btn-success" type="button" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add product</button>
  19. <br /><br />
  20. <form method="POST" action="">
  21. <div class="form-inline">
  22. <input type="date" class="form-control" name="from" required="required"/>
  23. <input type="date" class="form-control" name="to" required="required"/>
  24. <button class="btn btn-primary" name="search"><span class="glyphicon glyphicon-search"></span> Search</button>
  25. </div>
  26. </form>
  27. <br />
  28. <table class="table table-bordered">
  29. <thead class="alert-info">
  30. <tr>
  31. <th>Product name</th>
  32. <th>Brand</th>
  33. <th>Price</th>
  34. <th>Date Receive</th>
  35. </tr>
  36. </thead>
  37. <tbody><?php include 'search.php'?></tbody>
  38. </table>
  39. </div>
  40. <div class="modal fade" id="form_modal" aria-hidden="true">
  41. <div class="modal-dialog">
  42. <div class="modal-content">
  43. <form method="POST" action="save.php">
  44. <div class="modal-header">
  45. <h3 class="modal-title">Add Product</h3>
  46. </div>
  47. <div class="modal-body">
  48. <div class="col-md-2"></div>
  49. <div class="col-md-8">
  50. <div class="form-group">
  51. <label>Product name</label>
  52. <input type="text" class="form-control" name="product_name" required="required"/>
  53. </div>
  54. <div class="form-group">
  55. <label>Brand</label>
  56. <input type="text" class="form-control" name="brand" required="required"/>
  57. </div>
  58. <div class="form-group">
  59. <label>Price</label>
  60. <input type="number" min="0" class="form-control" name="price" required="required"/>
  61. </div>
  62. <div class="form-group">
  63. <label>Date Receive</label>
  64. <input type="date" class="form-control" name="date_receive" required="required"/>
  65. </div>
  66. </div>
  67. </div>
  68. <br style="clear:both;"/>
  69. <div class="modal-footer">
  70. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  71. <button class="btn btn-primary" name="save"><span class="glyphicon glyphicon-save"></span> Save</button>
  72. </div>
  73. </form>
  74. </div>
  75. </div>
  76. </div>
  77. <script src="js/jquery-3.2.1.min.js"></script>
  78. <script src="js/bootstrap.js"></script>
  79. </body>
  80. </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!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.