Share PHP to calculate the number of days between two dates code, _php tutorial

Source: Internet
Author: User
Tags date1 learn php programming

Share PHP to calculate the number of days between two dates code,


This example describes how PHP calculates the two-day difference between days. Share to everyone for your reference. The implementation method is as follows:

<?php$date1 = 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 * 36 5*60*60*24-$months *30*60*60*24)/(60*60*24));p rintf ("%d years,%d months,%d days\n", $years, $months, $days);--------- -----------------------------------------------or$date1 = new DateTime ("2007-03-24"), $date 2 = new DateTime (" 2009-06-26 "), $interval = $date 1->diff ($date 2); echo" Difference ". $interval->y. "Years,". $interval->m. "Months,". $interval->d. "Days"; Shows the total amount's (not divided to years, months and days like above) echo "difference". $interval->days.  "Days";--------------------------------------------------------or/** * Calculate differences between, dates with Precise semantics. Based on PHPs DateTime::d iff () * implementation by Derick Rethans. Ported to PHP by Emil H, 2011-05-02. No Rights reserved.*/function _date_range_limit ($start, $end, $adj, $a, $b, $result) {if ($result [$a] < $start) {$  result[$b]-= Intval (($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, 31, 3 0, 31); $days _in_month = Array (31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); _date_range_limit (1, +, "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, a, "s", "I", $result); $result = _date_range _limit (0, Max, "I", "H", $result); $result = _date_range_limit (0, a, "H", "D", $result); $result = _date_range_limit (0, N, a, "M", "Y", $result); $result = _date_range_limit_days (& $base, & $result); $result = _date_range_limit (0, N, a, "M", "Y", $result); return $result;} /** * accepts, 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 '
';p Rint_r (_date_diff (Strtotime ($date), Time ()), Echo '
';?>

I hope this article will help you learn PHP programming.

Articles you may be interested in:

    • PHP calculates two date time difference (returns year, month, day)
    • PHP calculates a 2-date difference function share
    • PHP calculates the two-day difference between days
    • PHP uses Strtotime to calculate the number of days between a given date two
    • Php method to determine how many months are different between two dates

http://www.bkjia.com/PHPjc/1084555.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084555.html techarticle share PHP to calculate the number of days between two dates of the code, the example of this article describes the PHP calculation of two days difference between the method. Share to everyone for your reference. The concrete implementation method is as follows: P ...

  • 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.