PHP date to date search.

We will do a simple code for date to date search in PHP. At first we need a design, lats make a design using bootstrap 4.

<!DOCTYPE html>
<html lang="en">
<head>
     <meta charset="UTF-8">
     <title>Document</title>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>

<div class="container">

<div class="row mt-5">
<div class="col-md-6 mx-auto"> 
<form action="" class="form-inline">

<div class="form-group">
       <input class="form-control" type="date" name="sdate" value=<?php echo $sdate; ?>>
       &nbsp
       <input class="form-control" type="date" name="edate" value=<?php echo $edate; ?>>
</div>
&nbsp
<button style="line-height: 1.9;" class="btn btn-info btn-sm">Search</button>

</form>
</div>
</div>

<div class="row mt-1">
<div class="col-md-6 mx-auto">
  <table class="table table-bordered">
      <tr>
        <th>TID</th>
        <th>Amount</th>
        <th>Date</th>
     </tr>
     <tr>
        <td>ABC</td>
        <td>100</td>
        <td>2019-11-18 02:11:07</td>
     </tr>
  </table>
</div>
</div>

</div>
</body>
</html>

Did you notice I have used type = date to get HTML date picker.
Now we need a simple PHP code to fetch all data.

Database informal connection and sql for get all data from transaction table.  We can write like below

$conn  =  new mysqli("localhost","root","","xeasy_accounting");
$sql = "SELECT * FROM transactions";
$result = $conn->query($sql);

For fetch data we can write

<?php foreach ($result as $value) { ?>

<tr>
   <td><?php echo $value['transaction_code']; ?></td>
   <td><?php echo $value['amount']; ?></td>
   <td><?php echo $value['created']; ?></td>
</tr>

<?php } ?>

Now we can GET start date and end date using GET.

$sdate = @$_GET['sdate'];
$edate = @$_GET['edate'];

Here I have used @ for avoid PHP notice. For keep Start date and End date in input field we can write like below code.

<input class="form-control" type="date" name="sdate" value=<?php echo $sdate; ?>>

<input class="form-control" type="date" name="edate" value=<?php echo $edate; ?>>

Now we can write our SQL like

$sql = "SELECT * FROM transactions WHERE date(created) BETWEEN '$sdate' AND '$edate'";

Finally our source will like below

<!DOCTYPE html>
<html lang="en">
<head>
     <meta charset="UTF-8">
     <title>Document</title>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>

<?php 
     $sdate = @$_GET['sdate'];
     $edate = @$_GET['edate'];
?>

<div class="container">

<div class="row mt-5">

<div class="col-md-6 mx-auto"> 
<form action="" class="form-inline">

<div class="form-group">
     <input class="form-control" type="date" name="sdate" value=<?php echo $sdate; ?>>
     &nbsp
     <input class="form-control" type="date" name="edate" value=<?php echo $edate; ?>>
</div>
     &nbsp
     <button style="line-height: 1.9;" class="btn btn-info btn-sm">Search</button>

</form>

</div>

</div>

<?php 
$conn = new mysqli("localhost","root","","xeasy_accounting");

if($sdate && $edate)
   $sql = "SELECT * FROM transactions WHERE date(created) BETWEEN '$sdate' AND '$edate'";
else
   $sql = "SELECT * FROM transactions";

$result = $conn->query($sql);
?>

<div class="row mt-1">
<div class="col-md-6 mx-auto">
<table class="table table-bordered">
<tr>
<th>TID</th>
<th>Amount</th>
<th>Date</th>
</tr>
<?php foreach ($result as $value) { ?>

<tr>
<td><?php echo $value['transaction_code']; ?></td>
<td><?php echo $value['amount']; ?></td>
<td><?php echo $value['created']; ?></td>
</tr>

<?php } ?>

</table>
</div>
</div>

</div>
</body>
</html>

Output :

Video Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *