How To Get All Dates Present Between Two Dates In PHP

admin_img Posted By Bajarangi soft , Posted On 27-11-2020

Using Strtotime function we can get dates which present between two days in php,So today we discuss how to get it.

How To Get All Dates Present Between Two Dates In PHP

Complete Code For Getting All Dates Present Between Two Dates In PHP.

<!DOCTYPE html>
<html>
<head>
    <title>How To Get All Dates Present Between Two Dates In PHP</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet">
</head>
<style>
    body {
        background: black;
    }
</style>
<body>
<body>
<div class="container">
    <br/><br/>
    <div class="text-center">
        <h1 id="color" style="color: white;">Get All Dates Present Between Two Dates In PHP
        </h1>
    </div>

    <div class="well">
        <form action="" method="post" enctype="multipart/form-data">
            <label>Enter From Date</label><br>
            <input type="date" name="date1" value=""><br><br>
            <label>Enter To Date</label><br>
            <input type="date" name="date2" value=""><br><br>
            <input type="submit" value="submit" class="btn btn-success" name="submit">
        </form>
        <?php
        if (isset($_POST['submit'])) {

            // input start and end date
            $Date1 = $_POST['date1'];
            $Date2 = $_POST['date2'];


            // Declare an empty array
            $array = array();

            // Use strtotime function
            $Variable1 = strtotime($Date1);
            $Variable2 = strtotime($Date2);

            // Use for loop to store dates into array
            // 86400 sec = 24 hrs = 60*60*24 = 1 day
            for ($currentDate = $Variable1; $currentDate <= $Variable2;
                 $currentDate += (86400)) {

                $Store = date('d-m-y', $currentDate);
                $array[] = $Store;
            }

            // Display the dates in array format
            print_r($array);

        }
        ?>
    </div>
</div>
</body>
</html>

Related Post