当前位置:首页 > PHP教程 > php日期 > 列表

分享PHP计算两个日期相差天数的代码

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-01 10:15:33 浏览: 评论:0 

这篇文章主要为大家分享了PHP计算两个日期差的代码,实例分析了php操作日期的技巧,需要的朋友可以参考下,本文实例讲述了php计算两个日期相差天数的方法,分享给大家供大家参考,具体实现方法如下:

  1. <?php 
  2. $date1 = date( 'Y-m-d' ); 
  3. $date2 = "2015-12-04"; 
  4. $diff = abs(strtotime($date2) - strtotime($date1)); 
  5. $years = floor($diff / (365*60*60*24)); 
  6. $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24)); 
  7. $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); 
  8. printf("%d years, %d months, %d days\n", $years, $months, $days); 
  9. -------------------------------------------------------- OR 
  10. $date1 = new DateTime("2007-03-24"); 
  11. $date2 = new DateTime("2009-06-26"); 
  12. $interval = $date1->diff($date2); 
  13. echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";  
  14. // shows the total amount of days (not divided into years, months and days like above) 
  15. echo "difference " . $interval->days . " days "; 
  16. -------------------------------------------------------- OR   
  17.     
  18. /** 
  19.  * Calculate differences between two dates with precise semantics. Based on PHPs DateTime::diff() 
  20.  * implementation by Derick Rethans. Ported to PHP by Emil H, 2011-05-02. No rights reserved. 
  21. */ 
  22. function _date_range_limit($start, $end, $adj, $a, $b, $result) 
  23. { 
  24.  if ($result[$a] < $start) { 
  25.   $result[$b] -= intval(($start - $result[$a] - 1) / $adj) + 1; 
  26.   $result[$a] += $adj * intval(($start - $result[$a] - 1) / $adj + 1); 
  27.  } 
  28.  if ($result[$a] >= $end) { 
  29.   $result[$b] += intval($result[$a] / $adj); 
  30.   $result[$a] -= $adj * intval($result[$a] / $adj); 
  31.  } 
  32.  return $result; 
  33. } 
  34. function _date_range_limit_days($base, $result) 
  35. { 
  36.  $days_in_month_leap = array(31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
  37.  $days_in_month = array(31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
  38.  _date_range_limit(1, 13, 12, "m", "y", &$base); 
  39.  $year = $base["y"]; 
  40.  $month = $base["m"]; 
  41.  if (!$result["invert"]) { 
  42.   while ($result["d"] < 0) { 
  43.    $month--; 
  44.    if ($month < 1) { 
  45.     $month += 12; 
  46.     $year--; 
  47.    } 
  48.    $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0); 
  49.    $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month]; 
  50.    $result["d"] += $days; 
  51.    $result["m"]--; 
  52.   } 
  53.  } else { 
  54.   while ($result["d"] < 0) { 
  55.    $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0); 
  56.    $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month]; 
  57.    $result["d"] += $days; 
  58.    $result["m"]--; 
  59.    $month++; 
  60.    if ($month > 12) { 
  61.     $month -= 12; 
  62.     $year++; 
  63.    } 
  64.   } 
  65.  } 
  66.  return $result; 
  67. } 
  68. function _date_normalize($base, $result) 
  69. { 
  70.  $result = _date_range_limit(0, 60, 60, "s", "i", $result); 
  71.  $result = _date_range_limit(0, 60, 60, "i", "h", $result); 
  72.  $result = _date_range_limit(0, 24, 24, "h", "d", $result); 
  73.  $result = _date_range_limit(0, 12, 12, "m", "y", $result); 
  74.  $result = _date_range_limit_days(&$base, &$result); 
  75.  $result = _date_range_limit(0, 12, 12, "m", "y", $result); 
  76.  return $result; 
  77. } 
  78. /** 
  79.  * Accepts two unix timestamps. 
  80.  */ 
  81. function _date_diff($one, $two) 
  82. { 
  83.  $invert = false; 
  84.  if ($one > $two) { 
  85.   list($one, $two) = array($two, $one); 
  86.   $invert = true; 
  87.  } 
  88.  $key = array("y", "m", "d", "h", "i", "s"); 
  89.  $a = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $one)))); 
  90.  $b = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $two)))); 
  91.  $result = array(); 
  92.  $result["y"] = $b["y"] - $a["y"]; 
  93.  $result["m"] = $b["m"] - $a["m"]; 
  94.  $result["d"] = $b["d"] - $a["d"]; 
  95.  $result["h"] = $b["h"] - $a["h"]; 
  96.  $result["i"] = $b["i"] - $a["i"]; 
  97.  $result["s"] = $b["s"] - $a["s"]; 
  98.  $result["invert"] = $invert ? 1 : 0; 
  99.  $result["days"] = intval(abs(($one - $two)/86400)); 
  100.  if ($invert) { 
  101.   _date_normalize(&$a, &$result); 
  102.  } else { 
  103.   _date_normalize(&$b, &$result); 
  104.  } 
  105.  return $result; 
  106. } 
  107. $date = "2014-12-04 19:37:22"; 
  108. echo '<pre>'; 
  109. print_r( _date_diff( strtotime($date), time() ) ); 
  110. echo '</pre>';  
  111. ?> 

希望本文所述对大家学习php程序设计有所帮助。

Tags: PHP计算两个日期相差天数

分享到:

相关文章