PHP: Return all dates between two dates in an array

PHP: Return all dates between two dates in an array

  1. Blog
  2. 15 November 2026
  3. 2 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP: Return all dates between two dates in an array

Generating a sequential list of dates between two specific points in time is a common requirement for calendar views, date pickers, or reporting logic. The goal is straightforward given inputs like '2010-10-01' and '2010-10-05': you need an array containing every day inclusive of the start and end dates. While this might seem trivial, handling leap years, month boundaries, and timezone nuances requires a deliberate approach to date manipulation in PHP.

The most robust solution leverages PHP's DateTime and DateInterval classes rather than string manipulation. String-based logic often leads to subtle bugs where edge cases like February 29th or month rollovers break the iteration logic. By creating two DateTime objects, you establish a solid foundation that handles calendar arithmetic automatically.

function getDatesFromRange($startDate, $endDate): array {
    $start = new DateTime($startDate);
    $end = new DateTime($endDate);
    
    $interval = new DateInterval('P1D');
    $iterator = new DateIterator($start);
    
    $dates = [];
    while ($iterator->getDate() <= $end->getTimestamp()) {
        $dates[] = $iterator->format('Y-m-d');
        $iterator->add($interval);
    }
    
    return $dates;
}

// Usage
print_r(getDatesFromRange('2010-10-01', '2010-10-05'));

This implementation uses a DateIterator initialized with the start date. It formats the current date into the ISO 8601 format (Y-m-d) and appends it to the result array before advancing the iterator by one day. The loop continues until the iterator's timestamp exceeds or equals the end date's timestamp. Using timestamps for the comparison ensures strict chronological ordering without relying on string comparison logic, which can fail if date formats vary slightly (e.g., including time components).

A critical trade-off in this approach is performance when dealing with very large ranges. While iterating one day at a time is efficient enough for standard business logic (months or years), calculating thousands of days might benefit from a different strategy. However, for typical UI needs like displaying a week or month view, the iterator pattern remains clear and readable.

Another consideration is input validation. The code above assumes valid date strings are passed. If $endDate precedes $startDate, the loop will not execute, returning an empty array. Depending on your application's requirements, you might want to throw an exception or return a specific error state in that scenario. Additionally, if you need to handle timezone offsets, initializing the DateTime objects without a default timezone can lead to unexpected results if the server environment defaults differ from the expected output zone.

For those working within the Laravel ecosystem, this logic translates easily into a service method or a helper trait. If you are building complex scheduling features or handling recurring events, frameworks like Laravel often provide extensive date helpers that wrap these core concepts. For instance, resources regarding advanced date manipulation and collection filtering can be found at https://laravelcompany.com, which may offer deeper integration patterns for such utility functions within a larger application architecture.

Ultimately, sticking to the native DateTime classes prevents reinventing the wheel with string parsing functions like strtotime() or manual date math, both of which are prone to edge-case failures without significant boilerplate code.

Our network