The example in this article describes how PHP calculates the number of days between two dates. Share to everyone for your reference. The implementation method is as follows:
<?php $date 1 = date (' y-m-d ');
$date 2 = "2015-12-04";
$diff = ABS (Strtotime ($date 2)-Strtotime ($date 1));
$years = Floor ($diff/(365*60*60*24));
$months = Floor (($diff-$years * 365*60*60*24)/(30*60*60*24));
$days = Floor (($diff-$years * 365*60*60*24-$months *30*60*60*24)/(60*60*24));
printf ("%d years,%d months,%d days\n", $years, $months, $days);
--------------------------------------------------------OR $date 1 = new DateTime ("2007-03-24");
$date 2 = new DateTime ("2009-06-26");
$interval = $date 1->diff ($date 2); echo "Difference". $interval->y. "Years," he said.
$interval->m. "Months," $interval->d. "Days"; Shows the total amount of days (not divided to years, months and days like above) echo "difference". $interval->days.
"Days"; --------------------------------------------------------OR/** * Calculate differences between two dates with Preci SE semantics. Based on Phps DateTime::d iff () * implementation by Derick Rethans. Ported to PHPby Emil H, 2011-05-02.
No rights reserved. */function _date_range_limit ($start, $end, $adj, $a, $b, $result) {if ($result [$a] < $start) {$result [$b] = Intv
Al (($start-$result [$a]-1)/$adj) + 1;
$result [$a] + + = $adj * Intval ($start-$result [$a]-1)/$adj + 1);
} if ($result [$a] >= $end) {$result [$b] + + = intval ($result [$a]/$ADJ);
$result [$a]-= $adj * Intval ($result [$a]/$ADJ);
return $result; function _date_range_limit_days ($base, $result) {$days _in_month_leap = array (31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 3
1, 30, 31);
$days _in_month = Array (31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
_date_range_limit (1, N, "M", "Y", & $base);
$year = $base ["Y"];
$month = $base ["M"];
if (! $result ["invert"]) {while ($result ["D"] < 0) {$month-;
if ($month < 1) {$month + = 12;
$year--; } $leapyear = $year% 400 = 0 | |
($year%!= 0 && $year% 4 = 0); $days = $leapyear? $days _in_month_leap[$month] : $days _in_month[$month];
$result ["D"] + = $days;
$result ["M"]--; } else {while ($result ["D"] < 0) {$leapyear = $year% 400 = 0 | |
($year%!= 0 && $year% 4 = 0); $days = $leapyear?
$days _in_month_leap[$month]: $days _in_month[$month];
$result ["D"] + = $days;
$result ["M"]--;
$month + +;
if ($month >) {$month-= 12;
$year + +;
}} return $result;
function _date_normalize ($base, $result) {$result = _date_range_limit (0, N, "s", "I", $result);
$result = _date_range_limit (0, M, "I", "H", $result);
$result = _date_range_limit (0, N, "H", "D", $result);
$result = _date_range_limit (0, a, "M", "Y", $result);
$result = _date_range_limit_days (& $base, & $result);
$result = _date_range_limit (0, a, "M", "Y", $result);
return $result;
}/** * accepts two UNIX timestamps.
*/function _date_diff ($one, $two) {$invert = false;
if ($one > $two) {list ($one, $two) = Array ($two, $one); $invert = true;
$key = Array ("Y", "M", "D", "H", "I", "s");
$a = Array_combine ($key, Array_map ("Intval", Explode ("", Date ("Y m D H i S", $one)));
$b = Array_combine ($key, Array_map ("Intval", Explode ("", Date ("Y m D H i S", $two)));
$result = Array ();
$result ["y"] = $b ["y"]-$a ["Y"];
$result ["M"] = $b ["M"]-$a ["M"];
$result ["d"] = $b ["D"]-$a ["D"];
$result ["h"] = $b ["h"]-$a ["H"];
$result ["i"] = $b ["i"]-$a ["I"];
$result ["s"] = $b ["s"]-$a ["s"]; $result ["invert"] = $invert?
1:0;
$result ["days"] = Intval (ABS ($one-$two)/86400);
if ($invert) {_date_normalize (& $a, & $result);
else {_date_normalize (& $b, & $result);
return $result;
} $date = "2014-12-04 19:37:22";
Echo ' <pre> ';
Print_r (_date_diff (Strtotime ($date), Time ()));
Echo ' </pre> ';
?>
Hopefully this article will help you learn about PHP programming.