How does PHP calculate and increase the total number of days from one day to one day?

Source: Internet
Author: User
For example, on March 13, we know the start and end of the day. How to calculate the intermediate date. For example, on March 13, we know the start and end of the day. How to calculate the intermediate date.

Reply content:

For example, on March 13, we know the start and end of the day. How to calculate the intermediate date.

Since the start and end time are known, the end time stamp minus the start time stamp difference divided by 86400 is the number of days

If it is to get the intermediate date, I think the upstairsSouth BirdThe code is not concise, as shown below:

$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')."
";}

We recommend the best PHP time processing extension I have used: Carbon. basically, all the time-related calculations can be done.

Carbon is 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.