For example September 15 September 16 September 17, know from which day starts and to which day ends. How to figure out the middle date.
Reply content:
For example September 15 September 16 September 17, know from which day starts and to which day ends. How to figure out the middle date.
Now that you know the start and end times, the difference between the end timestamp minus the start timestamp divided by 86400 is the number of days
If it is to get the intermediate date, I think 南小鸟
the code upstairs is not concise enough, as follows
$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')."
";}
Recommend the best php time processing extensions I've ever used: Carbon, basically all of the time calculations can be done.
Carbon more Convenient
However, PHP has this function by default.
$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) {}
$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) . "
";}2016-09-152016-09-162016-09-17