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

Source: Internet
Author: User
Tags echo date
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
  • Related Article

    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.