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); }?>