PHP gets the number of days between the specified date

Source: Internet
Author: User
This article mainly introduces PHP to get a given date difference between the days of the method, combined with a specific instance of the form of analysis of the 2 days of date difference calculation method, involving the PHP date string conversion of the relevant operation skills, the need for friends can refer to the next

The example in this article describes how PHP gets the number of days between a given date. Share to everyone for your reference, as follows:

Method One:

<?phpfunction count_days ($a, $b) {$a _dt=getdate ($a); $b _dt=getdate ($b); $a _new=mktime (12,0,0, $a _dt[' mon '), $a _dt[ ' Mday '], $a _dt[' year '); $b _new=mktime (12,0,0, $b _dt[' mon '), $b _dt[' Mday '], $b _dt[' year '); Return round (ABS ($a _new-$b _new)/86400);} The difference between today and August 26, 2017 $date1=strtotime (date ("y-m-d")), $date 2=strtotime (' 2017-8-26 '); $result =count_days ($date 1,$ DATE2); Echo $result;? >

Run Result: 187

Method Two:

<?php//today and August 26, 2017 How many days $date_1=date ("y-m-d"), $Date _2= "2017-8-26"; $d 1=strtotime ($Date _1); $d 2=strtotime ($ date_2); $Days =round (($d 2-$d 1)/3600/24); echo "Today differs from August 26, 2017". $Days. " Days ";? >

Operation Result:

Today differs from August 26, 2017 by 187 days

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.