Search code examples
phpdatetimedate-range

PHP: Return all dates between two dates in an array


Expected Input:

getDatesFromRange( '2010-10-01', '2010-10-05' );

Expected Output:

Array( '2010-10-01', '2010-10-02', '2010-10-03', '2010-10-04', '2010-10-05' )

Solution

  • You could also take a look at the DatePeriod class:

    $period = new DatePeriod(
         new DateTime('2010-10-01'),
         new DateInterval('P1D'),
         new DateTime('2010-10-05')
    );
    

    Which should get you an array with DateTime objects.

    To iterate

    foreach ($period as $key => $value) {
        //$value->format('Y-m-d')       
    }