How To Get All Dates Between Two Dates in PHP Tutorial

Share this Article
Reading Time: 3 minutes
4,777 Views

Inside this article we will see the concept to get all dates between two dates in php. Article contains classified information about getting date range in between two dates. There are several methods by the help of which we can get all available dates between two different dates. We will see the better and more simple way to get those.

If you are looking for an article which gives you the understanding of getting a date range between two dates in php then you are at right place to learn and get the things.

In several application somewhere we need a date range within two different dates, this article will help you to understand that concept.

Learn More –

Let’s get started.


Get All Dates in a Date Range

Create a file index.php at your localhost directory.

Open index.php and write this complete code into it.

<?php

function getBetweenDates($startDate, $endDate)
{
    $rangArray = [];

    $startDate = strtotime($startDate);
    $endDate = strtotime($endDate);

    for (
        $currentDate = $startDate;
        $currentDate <= $endDate;
        $currentDate += (86400)
    ) {

        $date = date('Y-m-d', $currentDate);
        $rangArray[] = $date;
    }

    return $rangArray;
}

$dates = getBetweenDates('2021-09-01', '2021-09-10');

echo "<pre>";
print_r($dates);

Output

Array
 (
     [0] => 2021-09-01
     [1] => 2021-09-02
     [2] => 2021-09-03
     [3] => 2021-09-04
     [4] => 2021-09-05
     [5] => 2021-09-06
     [6] => 2021-09-07
     [7] => 2021-09-08
     [8] => 2021-09-09
     [9] => 2021-09-10
 )

We hope this article helped you to learn How To Get All Dates Between Two Dates in PHP Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4