Return all dates between two dates in an array in PHP
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:
Table of Content
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.
<?php
// Function to get all the dates in given range
function getDatesFromRange($start, $end, $format = 'Y-m-d'){
// Declare an empty array
$array = array();
// Variable that store the date interval
// of period 1 day
$interval = new DateInterval('P1D');
$realEnd = new DateTime($end);
$realEnd->add($interval);
$period = new DatePeriod(new DateTime($start), $interval, $realEnd);
// Use loop to store date into array
foreach($period as $date){
$array[] = $date->format($format);
}
// Return the array elements
return $array;
}
// Function call with passing the start date and end date
$Date = getDatesFromRange('2010-10-01', '2010-10-05');
var_dump($Date);
?>
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.
<?php
// Declare two dates
$Date1 = '01-10-2010';
$Date2 = '05-10-2010';
// Declare an empty array
$array = array();
// Use strtotime function
$Variable1 = strtotime($Date1);
$Variable2 = strtotime($Date2);
// Use for loop to store dates into array
// 86400 sec = 24 hrs = 60*60*24 = 1 day
for ($currentDate = $Variable1;
$currentDate <= $Variable2;
$currentDate += (86400)){
$Store = date('Y-m-d', $currentDate);
$array[] = $Store;
}
// Display the dates in array format
print_r($array);
?>
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:
<?php
// Define start and end dates
$startDate = new DateTime('2023-01-01');
$endDate = new DateTime('2023-01-10');
// Create DatePeriod object
$interval = new DateInterval('P1D');
// 1 day interval
$dateRange = new DatePeriod($startDate, $interval, $endDate->modify('+1 day'));
// Collect dates in an array
$dates = [];
foreach ($dateRange as $date){
$dates[] = $date->format('Y-m-d');
}
// Print the array of dates
print_r($dates);
?>
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.
<?php
function getDatesBetween($startDate, $endDate) {
$dates = [];
$currentDate = strtotime($startDate);
$endDate = strtotime($endDate);
while ($currentDate <= $endDate) {
$dates[] = date('Y-m-d', $currentDate);
$currentDate = strtotime('+1 day', $currentDate);
}
return $dates;
}
// Example usage
$startDate = '2023-01-01';
$endDate = '2023-01-10';
$dates = getDatesBetween($startDate, $endDate);
print_r($dates);
?>
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
<?php
function getDatesBetween($startDate, $endDate, &$dates = array()) {
$dates[] = $startDate;
$nextDate = date('Y-m-d', strtotime($startDate . ' +1 day'));
if ($nextDate <= $endDate) {
getDatesBetween($nextDate, $endDate, $dates);
}
return $dates;
}
$startDate = '2024-01-01';
$endDate = '2024-01-05';
$dates = getDatesBetween($startDate, $endDate);
print_r($dates);
?>
Output
Array ( [0] => 2024-01-01 [1] => 2024-01-02 [2] => 2024-01-03 [3] => 2024-01-04 [4] => 2024-01-05 )