How to Get All Dates Between Two Dates in PHP?

In this post, we will give you very simple example how to get dates between two dates in PHP.

1. Code:

<?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-11-01', '2021-11-10');
    print_r($dates);
?>

2. Example:

Input:

Input: startDate 1: 2021-11-01
       endDate 2: 2021-11-10

Output:

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