How to Get All Dates Between Two Dates in PHP

How to Get All Dates Between Two Dates in PHP

Hello everyone, back again at porkaone. On this occasion we will learn how to get all the dates between two dates. Intrigued?, Come on, follow the details below.


To get all the dates of two specific dates we need a little trick. This function is very useful if you are making scheduling applications, attendance applications, how to get total workdays, total days off, work periods and many more. Here's a simple trick how to make it function.


How to Get All Dates Between Two Dates in PHP

Create a file named get_all_dates_between_two_dates.php in the htdocs folder. Follow the script below.



<?php $datesArray = []; $total_dates = 0; $startDate = strtotime("2022-10-01"); $endDate = strtotime("2023-01-21"); for ( $currentDate = $startDate; $currentDate < $endDate; $currentDate += (86400)) { $date = date('Y-m-d', $currentDate); //save into array $datesArray[] = $date; //count total dates between two dates $total_dates++; } //print total dates and all dates echo $total_dates." days <br><br>"; foreach ($datesArray as $key => $value) { echo ($key+1).". ".$value."<br>"; }


Run the script above in the browser application, if successful it will look like the image below.

Final Result



Ok, that's all for our tutorial this time about how to get all the dates between two specific dates. Hopefully this is useful and you can implement it in your project. If you have questions, please ask in the comments column below. That is all and thank you.

Post a Comment

0 Comments