Is there any PHP function to display all dates between two dates?
Asked
Active
Viewed 2.0k times
10
-
2No. Does this answer your question? – Jon Feb 13 '13 at 10:20
-
Can you add a sample output of what you expect ? More over, what have ou tried ? – Stephan Feb 13 '13 at 10:20
-
[Google.com](https://www.google.com/search?q=php+function+to+display+all+dates+between+two+date&aq=f&oq=php+function+to+display+all+dates+between+two+date&aqs=chrome.0.57j60l2j62j64.523&sourceid=chrome&ie=UTF-8). First link – Viktor S. Feb 13 '13 at 10:20
-
possible duplicate of [PHP: Return all dates between two dates in an array](http://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array) – Viktor S. Feb 13 '13 at 10:20
-
Hi FAngel: I asked any inbuild php function to calculate? – Kathiravan Feb 13 '13 at 10:21
-
May i know why u down voted my question? – Kathiravan Feb 13 '13 at 10:27
3 Answers
31
There is the DatePeriod
class.
EXAMPLE:
$begin = new DateTime('2013-02-01');
$end = new DateTime('2013-02-13');
$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
foreach($daterange as $date){
echo $date->format("Y-m-d") . "<br>";
}
(P1D stands for period of one day, see DateInterval
for further documentation)

Fabian Schmengler
- 24,155
- 9
- 79
- 111
-
2Almost there - dates from 2017-11-03 to 2017-11-10 show 03, 04, 05, 06, 07, 08, 09. We should either exclude 03 or include 10. – crafter Nov 10 '17 at 08:34
4
You can check out this function also
$day = 86400; // Day in seconds
$format = 'Y-m-d'; // Output format (see PHP date funciton)
$sTime = strtotime($start_date); // Start as time
$eTime = strtotime($end_date); // End as time
$numDays = round(($eTime - $sTime) / $day) + 1;
$days = array();
for ($d = 0; $d < $numDays; $d++) {
$days[] = date($format, ($sTime + ($d * $day)));
}

Venkata Krishna
- 4,287
- 6
- 30
- 53
-
-
1Notice that $day = 86400; is not always correct, there are shorter and longer days when summer time is applied. – Gabriel Reguly Aug 14 '17 at 16:49
-
@GabrielReguly is correct, 86400 is unreliable. If you build a calendar based on 86400 seconds per day you'll even get 28 oct 2018 twice. – Marcel Sep 20 '18 at 13:00
-
1
$start_date = '2020/09/01';
$end_date = '2020-09-03';
$new_date = new DateTime($end_date);
$new_date->add(new DateInterval('P1D'));
$end_date = $new_date->format('Y-m-d');
$period = new DatePeriod(
new DateTime($start_date),
new DateInterval('P1D'),
new DateTime($end_date)
);
print_r($period);
foreach ($period as $key => $value) {
echo $value->format('Y-m-d') .'<br>';
}
Print:
2020-09-01
2020-09-02
2020-09-03

Mahesh Kathiriya
- 612
- 7
- 9