Php calculates the time difference between two dates (year, month, and day) _ php instance

Source: Internet
Author: User
This article describes how to calculate the time difference between two dates (year, month, and day) in php, and how many days, months, and pre-dates in Weibo, for more information, see the PHP program. In most cases, you may encounter handling time problems, such as determining how long the user has been online and how many days have the user logged on to the system, the time difference between two posts or the log records between different operations. In this article, we will give an example of how PHP calculates the differences between the two dates: year, month, and day.

<? Php/** + ------------------------------------------------------------ * function: calculate the difference between the two dates: year, month, and day + period * @ param date $ date1 start date * @ param date $ date2 end date + period * @ return array + period */function DiffDate ($ date1, $ date2) {if (strtotime ($ date1)> s Trtotime ($ 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; $ y = round ($ math/12); $ m = intval ($ math % 12); $ 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) ;}?>

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.