Home > Article > Backend Development > How to calculate the total number of days from a certain day to a certain day in PHP and then increase it?
For example, September 15th, September 16th, September 17th, you know when it starts and when it ends. How to calculate the middle date.
For example, September 15th, September 16th, September 17th, you know when it starts and when it ends. How to calculate the middle date.
Now that we know the start and end times, the difference between the end timestamp minus the start timestamp divided by 86400 is the number of days between them
If it is to get the middle date, I think the code of Nan Xiaoniao
upstairs is not concise enough, as follows
<code>$start = new DateTime('2016-09-15'); $end = new DateTime('2016-09-17'); for ($start;$start<=$end;$start->modify('+1 day')) { echo $start->format('Y-m-d')."<br/>"; }</code>
Recommend the best PHP time processing extension I have ever used: Carbon, basically all time calculations can be done
Carbon is more convenient
but PHP has this function by default
<code>$start = new DateTime('2016-09-15'); $end = new DateTime('2016-09-17'); $inteval = new DateInteval('P1D'); $period = new DatePeriod($start, $end, $inteval); foreach ($period as $date) { }</code>
<code> $start = '2016-09-15'; $end = '2016-09-17'; $start = strtotime($start); $end = strtotime($end); $days = ($end - $start) / 3600 / 24; for ($i = 0; $i <= $days; $i++) { echo date('Y-m-d', $start + 3600 * 24 * $i) . "<br>"; } 2016-09-15 2016-09-16 2016-09-17 </code>