PHP Data Filter between Two Dates

PHP Data Filter between Two Dates, PHP Data Filter between Two Dates

Demo Available Here



You will learn how to filter or find data between two dates in PHP/MySQL in this project, which means you will be Selecting Data Between Two Dates in PHP/MySQL.

To begin, we'll create an Html form with input type=date and a submit button to submit the form. On form submission, you will search the database for data between two dates and display it in an Html table format developed with Bootstrap v5.

So, let's get started filtering or finding data in PHP Mysql between two dates STEP BY STEP:

 

Step-1: Create a Database 

 

"create database demo";

 

Also, See

Create Database phpMyAdmin

Create MySQL Database XAMPP Shell

CREATE DATABASE using PHP

 

Step-2: Create a Table

 

"CREATE table details (id int (11) AUTO_INCREMENT, name varchar (255), email varchar (255), reg_date date, primary key (id)) ";

 

Also, See

Create Database Table phpMyAdmin

Create Database Table XAMPP Shell

CREATE DATABASE Table using PHP

 

Step-3: Insert data into the table.

 

Create, Read, Update, and Delete (PHP CRUD)

 

Step-4: Database Connection (db_con.php).

 

$dbc = mysqli_connect("localhost","root","", "demo") or die ("could not connect to mysql"); 

 

Step-5: Html Form and PHP Code (index.php).

 

 

< ?php 
include_once 'db_con.php';

//query for select all data.

$sql = "SELECT * from details";
$run = mysqli_query($dbc, $sql);

? >
< !DOCTYPE html >
< html >
< head >
    < meta charset="utf-8" >
    < meta name="viewport" content="width=device-width, initial-scale=1" >
    < title >Data Filter between Two Dates< /title >
    < !-- CSS only -- >
< link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" >

< /head >
< body >
< section style="padding-top:50px;" >
    < div class="container" > 
        < div class="row" >
            < h1 >Data Filter between Two Dates< /h1 >
            < form method="post" action="index.php" >
                < br >
                < div class="col-lg-4" >
                    < div class="form-group" >
                        < input type="date" name="start_date" class="form-control" >
                    < /div >
                < /div >< br >
                < div class="col-lg-4" >
                    < div class="form-group" >
                        < input type="date" name="end_date" class="form-control" >
                    < /div >
                < /div >< br >
                < div class="col-lg-4" >
                    < div class="form-group" >
                        < input type="submit" name="submit_date" class="btn btn-primary" value="Filter" >
                    < /div >
                < /div >< br >
            < /form >
            
            < ?php 

                //fetch form data.
                if (isset($_POST['submit_date'])) {
                    
                    $start_date = $_POST['start_date'];
                    $end_date = $_POST['end_date'];
                           //select data between two dates.
                    $query = mysqli_query($dbc, "SELECT * from details where reg_date between '$start_date' and '$end_date' ");

                    if (mysqli_num_rows($query)>0) {? >
                        
                        

                        < div class="col-lg-12" >
                            < table class="table table-striped" >
                                < thead >
                                    < th >Name< /th >
                                    < th >Email< /th >
                                    < th >Date< /th >
                                < /thead >
                                < tbody >
                                < ?php 
                                     foreach ($query as $value) { 
                                         //print data between two dates.
                                 ? >
                                    < tr >
                                        < td >< ?=$value['name']? >< /td >
                                        < td >< ?=$value['email']? >< /td >
                                        < td >< ?=$value['reg_date']? >< /td >
                                    < /tr >
                                < ?php    } ? >
                                < /tbody >
                            < /table >
                        < /div >
                            
                    < ?php    
                    }else{

                        echo "No data found";
                    }

                }else{

                 //print all data
            ? >

            < div class="col-lg-12" > 

                < table class="table table-striped" >
                    < thead >
                        < th >#< /th >
                        < th >Name< /th >
                        < th >Email< /th >
                        < th >Reg. Date< /th >
                    < /thead >
                    < tbody >
                        < ?php 
                            $i=1;
                            while ($row=mysqli_fetch_assoc($run)) {
                                
                                $name = $row['name'];
                                $email = $row['email'];
                                $reg_date = $row['reg_date'];
                            
                        ? >
                        < tr >
                            < td >< ?=$i;? >< /td >
                            < td >< ?=$name;? >< /td >
                            < td >< ?=$email;? >< /td >
                            < td >< ?=$reg_date;? >< /td >
                        < /tr >
                    < ?php $i++; } ? >
                    < /tbody >
                    
                < /table >
                
            < /div >
        < ?php } ? >
        < /div >
    < /div >
< /section >
< /body >
< /html >

 

PHP Data Filter between Two Dates

 

Also, See

PHP Video Tutorials

PHP Projects

 

Download Source Code

Latest Updates

You May Also Like


PHP Projects

PHP CRUD Operation Project with Source Code

PHP CRUD Operation with MySQL & Bootstrap 5 (Create, Read, Update, Delete) - We will use INSERT, SELECT, UPDATE, and DELETE statements in MySQL, which equate to CRUD capability.....

JavaScript Projects

JavaScript Weight Converter Project with Source Code

JavaScript Weight Converter Download free with source code - Simple Weight Converter App using HTML & JavaScript that converts kilograms to other measurements.....

PHP Projects

PHP Multiple Image Upload Project with Source Code

Multiple Image Upload using PHP and MySQL download free with source code - 1. Create Database and Table. 2. Create HTML Form. 3. PHP Code for Upload an Image and PHP Code for Display an Image.....

JavaScript Projects

JavaScript Stopwatch Project with Source Code

JavaScript Stopwatch Download with source code - 1. Html code for timer and buttons. 2. Javascript code for set timer, hr min, sec and start, stop and reset.....

PHP Projects

PHP Image Upload Project with Source Code

Image Upload using PHP and MYSQL download free project with source code - 1. Create Database and Table. 2. Create HTML Form. 3. PHP Code for Upload an Image and PHP Code for Display an Image.....