How to Get All Dates Between Two Dates in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hi,

This example is focused on php get dates between two dates. you will learn how to get all dates between two dates in php. if you have question about return all dates between two dates in an array in php then i will give simple example with solution. if you want to see example of php get dates between two dates then you are a right place. follow bellow step for php date between range.

I will give you very simple example how to get dates between two dates in PHP. so let's see both example with output:

Example:

index.php

<?php

/**

* Write code on Method

*

* @return response()

*/

function getBetweenDates($startDate, $endDate)

{

$rangArray = [];

$startDate = strtotime($startDate);

$endDate = strtotime($endDate);

for ($currentDate = $startDate; $currentDate <= $endDate;

$currentDate += (86400)) {

$date = date('Y-m-d', $currentDate);

$rangArray[] = $date;

}

return $rangArray;

}

$dates = getBetweenDates('2021-11-01', '2021-11-10');

print_r($dates);

?>

Output:

Array

(

[0] => 2021-11-01

[1] => 2021-11-02

[2] => 2021-11-03

[3] => 2021-11-04

[4] => 2021-11-05

[5] => 2021-11-06

[6] => 2021-11-07

[7] => 2021-11-08

[8] => 2021-11-09

[9] => 2021-11-10

)

i hope it can help you...

Tags :
Shares