Skip to main content

Search Two Dates In Data Table Using PHP


Good Day!!!

If you are looking for source code in Search Two Dates In Data Table Using PHP then you are at the right place. In this article, we create a simple program that will teach you on how to create searching data use by two dates in PHP/MySQL. Let's begin with this:

Creating our Table

For the first step, we are going to make our database.
To create a database:

  1. Open PHPMyAdmin
  2. Create a database and name it as "lms_1".
  3. After creating a database name, click the SQL and kindly copy the code below.

  1. --
  2. -- Table structure for table `user`
  3. --
  4.  
  5. CREATE TABLE IF NOT EXISTS `user` (
  6. `user_id` int(11) NOT NULL AUTO_INCREMENT,
  7. `school_number` varchar(100) NOT NULL,
  8. `firstname` varchar(100) NOT NULL,
  9. `middlename` varchar(100) NOT NULL,
  10. `lastname` varchar(100) NOT NULL,
  11. `contact` varchar(100) NOT NULL,
  12. `gender` varchar(100) NOT NULL,
  13. `address` varchar(100) NOT NULL,
  14. `type` varchar(100) NOT NULL,
  15. `level` varchar(100) NOT NULL,
  16. `section` varchar(100) NOT NULL,
  17. `user_image` varchar(100) NOT NULL,
  18. `status` varchar(100) NOT NULL,
  19. `user_added` datetime NOT NULL,
  20. PRIMARY KEY (`user_id`)

Creating The Form Field

This form field where the user can search data using two dates.

  1. <form method="POST" action="user_log_search.php" class="form-inline">
  2. <div class="control-group">
  3. <div class="controls">
  4. <div class="col-md-3">
  5. <input type="date" style="color:black;" value="<?php echo date('Y-m-d'); ?>" name="datefrom" class="form-control has-feedback-left" placeholder="Date From" aria-describedby="inputSuccess2Status4" required />
  6. <span class="fa fa-calendar-o form-control-feedback left" aria-hidden="true"></span>
  7. <span id="inputSuccess2Status4" class="sr-only">(success)</span>
  8. </div>
  9. </div>
  10. </div>
  11. <div class="control-group">
  12. <div class="controls">
  13. <div class="col-md-3">
  14. <input type="date" style="color:black;" value="<?php echo date('Y-m-d'); ?>" name="dateto" class="form-control has-feedback-left" placeholder="Date To" aria-describedby="inputSuccess2Status4" required />
  15. <span class="fa fa-calendar-o form-control-feedback left" aria-hidden="true"></span>
  16. <span id="inputSuccess2Status4" class="sr-only">(success)</span>
  17. </div>
  18. </div>
  19. </div>
  20. <button type="submit" name="search" class="btn btn-primary btn-outline"><i class="fa fa-calendar-o"></i> Search By Date Log In</button>
  21. </form>

Creating Connection

This PHP source code is our database connection, kindly copy and save it as "dbcon.php".

  1. <?php
  2. mysql_select_db('lms_1',mysql_connect('localhost','root',''))or die(mysql_error());
  3. ?>

Our Search Script

This source code will execute after the user set the two dates to search data in the data table.

  1. <?php
  2. $datefrom = $_POST['datefrom'];
  3. $dateto = $_POST['dateto'];
  4. $result= mysql_query("select * from user_log
  5. LEFT JOIN user ON user_log.user_id = user.user_id
  6. where user_log.date_log BETWEEN '".$_POST['datefrom']." 00:00:01' and '".$_POST['dateto']." 23:59:59'
  7. order by user_log.user_log_id DESC ") or die (mysql_error());
  8. while ($row= mysql_fetch_array ($result) ){
  9. $id=$row['user_log_id'];
  10. $user_id=$row['user_id'];
  11. ?>

And, this is the output:

Result

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.