Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn to calculate the difference between the two dates: year, month, and day.
Year, month, and day
DiffDate ("2011-01-06", "2015-06-16 ");
/**
+ ----------------------------------------------------------
* Function: calculate the difference between two dates: year, month, and day.
+ ----------------------------------------------------------
* @ Param date $ date1 start date
* @ Param date $ date2 end date
+ ----------------------------------------------------------
* @ Return array
+ ----------------------------------------------------------
*/
Function DiffDate ($ date1, $ date2 ){
If (strtotime ($ date1)> strtotime ($ date2 )){
$ Ymd = $ date2;
$ Date2 = $ date1;
$ Date1 = $ ymd;
}
List ($ y1, $ m1, $ d1) = explode ('-', $ date1 );
List ($ y2, $ m2, $ d2) = explode ('-', $ date2 );
$ Y = $ m = $ d =$ _ m = 0;
$ Math = ($ y2-$ y1) * 12 + $ m2-$ m1; // The total number of months for difference
$ Y = round ($ math/12); // year of difference
$ M = intval ($ math % 12); // number of different months
$ Mm = $ math; // The total number of different months
$ D = (mktime (0, 0, 0, $ m2, $ d2, $ y2)-mktime (0, 0, 0, $ m2, $ d1, $ y2 )) /86400;
If ($ d <0 ){
$ M-= 1;
$ D + = date ('J', mktime (0, 0, 0, $ m2, 0, $ y2 ));
}
$ M <0 & $ y-= 1;
Return array ($ y, $ m, $ d, $ mm );
}
Calculated by day, hour, and second
$ One = strtotime ('2017-12-08 07:02:40 '); // start time stamp
$ Tow = strtotime ('2017-12-25 00:00:00 '); // End Time Stamp
$ Cle = $ tow-$ one; // get the timestamp difference
/* This is just a prompt
Echo floor ($ cle/60); // returns the total number of minutes
Echo floor ($ cle/3600); // calculates the total number of hours
Echo floor ($ cle/3600/24); // returns the total number of days
*/
/* Rming () function, that is, the Rounding Method */
$ D = floor ($ cle/3600/24 );
$ H = floor ($ cle % (3600*24)/3600); // % obtain the remainder
$ M = floor ($ cle % (3600*24) % 3600/60 );
$ S = floor ($ cle % (3600*24) % 60 );
Echo "two time differences $ d days $ h hours $ m minutes $ s seconds"
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB