How to get all the dates between the two dates using PHP ?

To get the dates between the two dates , we will simply convert the date string to time using strtotime() function then we can print the all the dates between two dates using for loop.

PHP code to print and store all the dates between two dates

Now we are going to pass the two dates in strtotime() function.Using for loop we will calculate the dates.As we know that one day has 86400 seconds, so we will take the increment 86400 after every one loop. Like. $i=$i+86400;

$array =array(); $date_from = strtotime("2018-11-01"); $date_to = strtotime("2018-11-05"); for ($i=$date_from; $i<=$date_to; $i+=86400) { array_push($array, date("Y-m-d H:i:s", $i)); } print_r($array);

Output of returning all dates between two dates in an array

We can get all any date and time in any format using date() function. E.g date('Y-m-d') , date('Y-m-d H:i:s') etc.

Array ( [0] => 2018-11-01 00:00:00 [1] => 2018-11-02 00:00:00 [2] => 2018-11-03 00:00:00 [3] => 2018-11-04 00:00:00 [4] => 2018-11-05 00:00:00 )
Chapter Next »