PHP calculates the day interval

Source: Internet
Author: User

At the beginning, when I did not check the PHP manual, I had to use the old method,CodeIs implemented in this way:

 

 $ Date_1 = Date ('Y-m-d' );  $ Date_2 = '2017-07-16' ;  $ Date1_arr = Explode ("-", $ Date_1  );  $ Date2_arr = Explode ("-",$ Date_2  );  $ Day1 = Mktime (0, 0, 0, $ Date1_arr [1], $ Date1_arr [2], $ Date1_arr [0 ]);  $ Day2 = Mktime (0, 0, 0, $ Date2_arr [1], $ Date2_arr [2], $ Date2_arr [0]);  $ Days = Round (( $ Day2 - $ Day1 )/3600/24 );  Echo   $ Days ; Exit ;

 

Then google. It is found that there is a date_diff method in the PHP manual, that is, the datetime class is instantiated, And the diff method is called: PHP version> = 5.3 is valid.

 

<? PHP  $ Datetime1 =New Datetime ('2017-10-11' );  $ Datetime2 = New Datetime ('2017-10-13' );  $ Interval = $ Datetime1 -> Diff ( $ Datetime2  );  Echo   $ Interval -> Format ('% R % A days' ); ?> <? PHP  $ Datetime1 = Date_create ('2017-10-11' );  $ Datetime2 = Date_create ('2017-10-13' );  $ Interval = Date_diff ( $ Datetime1 , $ Datetime2  );  Echo   $ Interval -> Format ('% R % A days' ); ?>

 

Both methods can be implemented.

Address: http://blog.csdn.net/jucrazy/article/details/6605890

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.