VOOZH about

URL: https://www.geeksforgeeks.org/php/return-all-dates-between-two-dates-in-an-array-in-php/

⇱ Return all dates between two dates in an array in PHP - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Return all dates between two dates in an array in PHP

Last Updated : 15 Jul, 2024

Returning all dates between two dates in an array means generating a list of all consecutive dates from the start date to the end date, inclusive, and storing each date as an element in an array for easy access.

Here we have some common methods:

Using DatePeriod Class

The DatePeriod class in PHP iterates over a range of dates using `DateTime` objects for start and end dates and a `DateInterval` for the interval. It generates a sequence of formatted dates between the specified start and end dates.

Example: In this example, use the date interval class which stores a fixed amount of time (in years, months, days, hours, etc) or relative time string in the format that DateTime.


Output
array(5) {
 [0]=>
 string(10) "2010-10-01"
 [1]=>
 string(10) "2010-10-02"
 [2]=>
 string(10) "2010-10-03"
 [3]=>
 string(10) "2010-10-04"
 [4]=>
 string(10) "2010-10-05"
}

Using strtotime() Function

The strtotime() function in PHP converts date strings to UNIX timestamps. By looping from the start date to the end date, incrementing by one day, and formatting each timestamp back to `'Y-m-d'`, it generates a sequence of dates.

Example 2:This example use strtotime() function which is used to convert an English textual date-time description to a UNIX timestamp. It returns a timestamp on success, False otherwise.


Output
Array
(
 [0] => 2010-10-01
 [1] => 2010-10-02
 [2] => 2010-10-03
 [3] => 2010-10-04
 [4] => 2010-10-05
)

Using DatePeriod Class

To generate an array of dates between two specified dates in PHP, utilize DatePeriod with DateTime objects for precise date iteration or employ a loop with `strtotime()` for straightforward date generation, formatting each date into `'Y-m-d'` and collecting them into an array.

Example:


Output
Array
(
 [0] => 2023-01-01
 [1] => 2023-01-02
 [2] => 2023-01-03
 [3] => 2023-01-04
 [4] => 2023-01-05
 [5] => 2023-01-06
 [6] => 2023-01-07
 [7] => 2023-01-08
 [8] => 2023-...

Using a While Loop

This approach involves creating a DateTime object for the start date, then repeatedly adding one day to this object and storing each date in an array until the end date is reached.

Example: In this example, the getDatesBetween function takes two parameters: the start date and the end date. It initializes the current date to the start date and uses a while loop to iterate from the start date to the end date. Each date is formatted to 'Y-m-d' and added to the array.


Output
Array
(
 [0] => 2023-01-01
 [1] => 2023-01-02
 [2] => 2023-01-03
 [3] => 2023-01-04
 [4] => 2023-01-05
 [5] => 2023-01-06
 [6] => 2023-01-07
 [7] => 2023-01-08
 [8] => 2023-...

Using Recursive Function

A recursive function in PHP can recursively generate dates between a given start and end date. It starts with the initial date, adds it to an array, calculates the next date, and continues until reaching or exceeding the end date, ensuring all dates are included in sequence.

Example


Output
Array
(
 [0] => 2024-01-01
 [1] => 2024-01-02
 [2] => 2024-01-03
 [3] => 2024-01-04
 [4] => 2024-01-05
)
Comment
Article Tags: