PHP calculation Two date Difference Month Day

Source: Internet
Author: User
In the PHP program, a lot of times will encounter the problem of processing time, such as: Determine how long the user online, a total number of days of login, two posts published the difference between the different operations of the log and so on. In this article, we give an example of how PHP calculates the two-day difference between a year, a month, and a day.

<?php/** +----------------------------------------------------------* Function: Calculates the date of the month of two days +------------------- ---------------------------------------* @param date $date 1 start date * @param date $date 2 deadline date +------------- ---------------------------------------------* @return Array +------------------------------------------- ---------------*/function diffdate ($date 1, $date 2) {if (Strtotime ($date 1) > Strtotime ($date 2)) {$ymd =         $date 2;         $date 2 = $date 1;     $date 1 = $ymd;     } list ($y 1, $m 1, $d 1) = Explode ('-', $date 1);     List ($y 2, $m 2, $d 2) = Explode ('-', $date 2);     $y = $m = $d = $_m = 0;     $math = ($y 2-$y 1) * + $m 2-$m 1;     $y = Round ($math/12);     $m = intval ($math% 12);     $d = (mktime (0, 0, 0, $m 2, $d 2, $y 2)-mktime (0, 0, 0, $m 2, $d 1, $y 2)/86400;         if ($d < 0) {$m-= 1;     $d + = date (' J ', Mktime (0, 0, 0, $m 2, 0, $y 2));     } $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.