How To Get Numbers of Days Names Between Two Dates In PHP

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

using php we can get numbers of days names which is present between two dates.So today we implement code to get days names presents between two days using php

How To Get Numbers of Days Names Between Two Dates In PHP

Complete Code For Getting Numbers of Days Names Between Two Dates In PHP
 

<!DOCTYPE html>
<html>
<head>
    <title>How To Get Numbers of Days Names 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;">Numbers of Days Names 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
            $startDate = $_POST['date1'];
            $endDate = $_POST['date2'];

            $resultDays = array('Monday' => 0,
                'Tuesday' => 0,
                'Wednesday' => 0,
                'Thursday' => 0,
                'Friday' => 0,
                'Saturday' => 0,
                'Sunday' => 0);

            // change string to date time object
            $startDate = new DateTime($startDate);
            $endDate = new DateTime($endDate);

            // iterate over start to end date
            while($startDate <= $endDate ){
                // find the timestamp value of start date
                $timestamp = strtotime($startDate->format('d-m-Y'));

                // find out the day for timestamp and increase particular day
                $weekDay = date('l', $timestamp);
                $resultDays[$weekDay] = $resultDays[$weekDay] + 1;

                // increase startDate by 1
                $startDate->modify('+1 day');
            }

            print_r($resultDays);

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

Related Post