How to get data from the database between two dates in PHP & MySQL
In this tutorial, we will learn how to get employee data between two dates in PHP from the database. Below is the step-by-step procedure.

Step1: First create a database with name “betdb” where employee data is stored.
Step2: Second is to create a table with the name “tblempdata” and insert the employee data.
1 2 3 4 5 6 7 8 9 10 |
CREATE TABLE `tblempdata` ( `ID` int(5) NOT NULL, `EmployeeName` varchar(200) DEFAULT NULL, `Department` varchar(200) DEFAULT NULL, `Email` varchar(200) DEFAULT NULL, `MobileNumber` varchar(200) DEFAULT NULL, `EmpID` varchar(200) DEFAULT NULL, `JoiningDate` date DEFAULT current_timestamp(), `PostingDate` timestamp NULL DEFAULT current_timestamp() ) ENGINE=InnoDB DEFAULT CHARSET=latin1; |
Step3: Create a database connection file and save this file with the name “config.php”. Below is the code of databse connection.
1 2 3 4 5 6 7 8 |
<?php $con = mysqli_connect("localhost","root","","betdb"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?> |
Step4: Create a form with two fields which is “from date” and “to date”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<form name="bwdatesdata" action="" method="post" action=""> <table width="100%" height="117" border="0"> <tr> <th width="27%" height="63" scope="row">From Date :</th> <td width="73%"> <input type="date" name="fdate" class="form-control" id="fdate"> </td> </tr> <tr> <th width="27%" height="63" scope="row">To Date :</th> <td width="73%"> <input type="date" name="tdate" class="form-control" id="tdate"></td> </tr> <tr> <th width="27%" height="63" scope="row"></th> <td width="73%"> <button class="btn-primary btn" type="submit" name="submit">Submit</button> </tr> </table> </form> |
Step 5: PHP code for fetching data from the database on the basis of from & to dates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<?php if(isset($_POST['submit'])) { $fdate=$_POST['fdate']; $tdate=$_POST['tdate']; ?> <h3 style="padding-left: 100px;color:blue">Report from <?php echo $fdate?> to <?php echo $tdate?></h3> <hr > <div class="row"> <table class="table table-bordered" width="100%" border="0" style="padding-left:40px"> <thead> <tr> <th scope="col">S.NO</th> <th scope="col">Employee Name</th> <th scope="col">Department</th> <th scope="col">Mobile Number</th> <th scope="col">Email</th> <th scope="col">Employee ID</th> <th scope="col">Joining Date</th> </tr> </thead> <?php $ret=mysqli_query($con,"select * from tblempdata where JoiningDate between '$fdate' and '$tdate' "); $num=mysqli_num_rows($ret); if($num>0){ $cnt=1; while ($row=mysqli_fetch_array($ret)) { ?> <tbody> <tr data-expanded="true"> <td><?php echo $cnt;?></td> <td><?php echo $row['EmployeeName'];?></td> <td><?php echo $row['Department'];?></td> <td><?php echo $row['MobileNumber'];?></td> <td><?php echo $row['Email'];?></td> <td> <?php echo $row['EmpID'];?></td> <td><?php echo $row['JoiningDate'];?></td> </tr> <?php $cnt=$cnt+1; } } else { ?> <tr> <td colspan="8"> No record found against this dates</td> </tr> <?php } }?> </tbody> </table> |
Here is the full code that we have written for this tutorial:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
<?php require_once("config.php"); ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <meta charset="utf-8"> <title>PHP GURUKUL | DEMO</title> <meta name="generator" content="Bootply" /> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> <link href="css/bootstrap.min.css" rel="stylesheet"> <!--[if lt IE 9]> <script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link href="css/styles.css" rel="stylesheet"> </head> <body> <nav class="navbar navbar-default navbar-fixed-top" role="navigation"> <div class="navbar-header"> <h4 style="padding-left: 100px;padding-top: 20px;">PHP GURUKUL | Programming Blog</h4> </div> </nav> <div class="container-fluid"> <!--center--> <div class="col-sm-8"> <div class="row"> <div class="col-xs-12"> <h3 style="padding-left: 100px;">How to get data from database between two dates in php and MySQL</h3> <hr > <form name="bwdatesdata" action="" method="post" action=""> <table width="100%" height="117" border="0"> <tr> <th width="27%" height="63" scope="row">From Date :</th> <td width="73%"> <input type="date" name="fdate" class="form-control" id="fdate"> </td> </tr> <tr> <th width="27%" height="63" scope="row">To Date :</th> <td width="73%"> <input type="date" name="tdate" class="form-control" id="tdate"></td> </tr> <tr> <th width="27%" height="63" scope="row"></th> <td width="73%"> <button class="btn-primary btn" type="submit" name="submit">Submit</button> </tr> </table> </form> </div> </div> <hr> <div class="row"> <div class="col-xs-12"> <?php if(isset($_POST['submit'])) { $fdate=$_POST['fdate']; $tdate=$_POST['tdate']; ?> <h3 style="padding-left: 100px;color:blue">Report from <?php echo $fdate?> to <?php echo $tdate?></h3> <hr > <div class="row"> <table class="table table-bordered" width="100%" border="0" style="padding-left:40px"> <thead> <tr> <th scope="col">S.NO</th> <th scope="col">Employee Name</th> <th scope="col">Department</th> <th scope="col">Mobile Number</th> <th scope="col">Email</th> <th scope="col">Employee ID</th> <th scope="col">Joining Date</th> </tr> </thead> <?php $ret=mysqli_query($con,"select * from tblempdata where JoiningDate between '$fdate' and '$tdate' "); $num=mysqli_num_rows($ret); if($num>0){ $cnt=1; while ($row=mysqli_fetch_array($ret)) { ?> <tbody> <tr data-expanded="true"> <td><?php echo $cnt;?></td> <td><?php echo $row['EmployeeName'];?></td> <td><?php echo $row['Department'];?></td> <td><?php echo $row['MobileNumber'];?></td> <td><?php echo $row['Email'];?></td> <td> <?php echo $row['EmpID'];?></td> <td><?php echo $row['JoiningDate'];?></td> </tr> <?php $cnt=$cnt+1; } } else { ?> <tr> <td colspan="8"> No record found against this dates</td> </tr> <?php } }?> </tbody> </table> </div> </div> </div> </div><!--/center--> <hr> </div><!--/container-fluid--> <!-- script references --> <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <script src="js/bootstrap.min.js"></script> </body> </html> |
Download Script
How to Run the Script
1. Download the zip file
2. Extract the file and copy bw_dates _script_php folder
3.Paste inside root directory(for xampp xampp/htdocs, for wamp wamp/www, for lamp var/www/HTML)
4.Open PHPMyAdmin (http://localhost/phpmyadmin)
5. Create a database with the name betdb
6. Import betdb.sql file(given inside the zip package in the SQL file folder)
7. Run the script http://localhost/bw_dates _script_php