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