Get all dates between two dates in PHP


To get all the dates between two given dates in PHP, We can use the DateInterval() class, which returns an array containing all dates between the given two dates.

Here is an example of getting all dates from a given date range:
Example
<?php 
  $fromDate = "2023-12-28";
  $toDate = "2024-01-10";

  $period = new DatePeriod(
    new DateTime($fromDate),
    new DateInterval('P1D'),
    new DateTime($toDate )
  );
   
  foreach ($period as $date) {
    echo $date->format('Y-m-d');
  }
?>

Output

2023-12-28
2023-12-29
2023-12-30
2023-12-31
2024-01-01
2024-01-02
2024-01-03
2024-01-04
2024-01-05
2024-01-06
2024-01-07
2024-01-08
2024-01-09