How to calculate the total number of days from a certain day to a certain day in PHP and then increase it?

WBOY
Release: 2016-10-12 10:04:07
Original
1545 people have browsed it

For example, September 15th, September 16th, September 17th, you know when it starts and when it ends. How to calculate the middle date.

Reply content:

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>
Copy after login

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>
Copy after login

<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>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template