Calculate the difference between two dates: year, month, and day

Source: Internet
Author: User
Tags date1
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

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.