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


Posted in PHP onDecember 23, 2015

本文实例讲述了php计算两个日期相差天数的方法。分享给大家供大家参考。具体实现方法如下:

<?php
$date1 = date( 'Y-m-d' );
$date2 = "2015-12-04";
$diff = abs(strtotime($date2) - strtotime($date1));
$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
$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days "; 
// shows the total amount of days (not divided into years, months and days like above)
echo "difference " . $interval->days . " days ";
-------------------------------------------------------- OR  
  
/**
 * Calculate differences between two dates with precise semantics. Based on PHPs DateTime::diff()
 * 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, 30, 31);
 $days_in_month = array(31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
 _date_range_limit(1, 13, 12, "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 % 100 != 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 % 100 != 0 && $year % 4 == 0);
   $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month];
   $result["d"] += $days;
   $result["m"]--;
   $month++;
   if ($month > 12) {
    $month -= 12;
    $year++;
   }
  }
 }
 return $result;
}
function _date_normalize($base, $result)
{
 $result = _date_range_limit(0, 60, 60, "s", "i", $result);
 $result = _date_range_limit(0, 60, 60, "i", "h", $result);
 $result = _date_range_limit(0, 24, 24, "h", "d", $result);
 $result = _date_range_limit(0, 12, 12, "m", "y", $result);
 $result = _date_range_limit_days(&$base, &$result);
 $result = _date_range_limit(0, 12, 12, "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>'; 
?>

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

PHP 相关文章推荐
php&amp;java(三)
Oct 09 PHP
PHP 最大运行时间 max_execution_time修改方法
Mar 08 PHP
php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串
Apr 05 PHP
PHP中Session引起的脚本阻塞问题解决办法
Apr 08 PHP
PHP常用的缓存技术汇总
May 05 PHP
PHP实现linux命令tail -f
Feb 22 PHP
thinkphp3.x中变量的获取和过滤方法详解
May 20 PHP
PHP实现多关键字加亮功能
Oct 21 PHP
/etc/php-fpm.d/www.conf 配置注意事项
Feb 04 PHP
利用PHP_XLSXWriter代替PHPExcel的方法示例
Jul 16 PHP
PHP+MySQL实现消息队列的方法分析
May 09 PHP
php实现微信支付之企业付款
May 30 PHP
php获得客户端浏览器名称及版本的方法(基于ECShop函数)
Dec 23 #PHP
PHP+MySQL实现无极限分类栏目的方法
Dec 23 #PHP
PHP多维数组转一维数组的简单实现方法
Dec 23 #PHP
详解WordPress中简码格式标签编写的基本方法
Dec 22 #PHP
WordPress中转义HTML与过滤链接的相关PHP函数使用解析
Dec 22 #PHP
WordPres对前端页面调试时的两个PHP函数使用小技巧
Dec 22 #PHP
WordPress主题中添加文章列表页页码导航的PHP代码实例
Dec 22 #PHP
You might like
php的$_FILES的临时储存文件与回收机制实测过程
2013/07/12 PHP
那些年我们错过的魔术方法(Magic Methods)
2014/01/14 PHP
PHP获取数组的键与值方法小结
2015/06/13 PHP
Linux系统中为php添加pcntl扩展
2016/08/28 PHP
PHP实现的迪科斯彻(Dijkstra)最短路径算法实例
2017/09/16 PHP
Bootstrap+PHP实现多图上传功能实例详解
2018/04/08 PHP
谷歌浏览器 insertCell与appendChild的区别
2009/02/12 Javascript
juqery 学习之六 CSS--css、位置、宽高
2011/02/11 Javascript
仿谷歌主页js动画效果实现代码
2013/07/14 Javascript
极易被忽视的javascript面试题七问七答
2016/02/15 Javascript
js判断数组key是否存在(不用循环)的简单实例
2016/08/03 Javascript
移动端js图片查看器
2016/11/17 Javascript
自己封装的一个原生JS拖动方法(推荐)
2016/11/22 Javascript
webpack构建的详细流程探底
2018/01/08 Javascript
微信小程序自定义音乐进度条的实例代码
2018/08/28 Javascript
详解vue高级特性
2020/06/09 Javascript
基于vue中的scoped坑点解说
2020/09/04 Javascript
Vue项目开发常见问题和解决方案总结
2020/09/11 Javascript
Vue-Ant Design Vue-普通及自定义校验实例
2020/10/24 Javascript
python持久性管理pickle模块详细介绍
2015/02/18 Python
python 使用poster模块进行http方式的文件传输到服务器的方法
2019/01/15 Python
Python实现封装打包自己写的代码,被python import
2020/07/12 Python
Cpython解释器中的GIL全局解释器锁
2020/11/09 Python
python3代码输出嵌套式对象实例详解
2020/12/03 Python
科颜氏加拿大官方网站: Kiehl’s加拿大
2016/08/16 全球购物
澳大利亚玩具剧场:Toy Playhouse
2019/03/03 全球购物
一套VC试题
2015/01/23 面试题
应付会计岗位职责
2013/12/12 职场文书
2014年大学教师工作总结
2014/12/02 职场文书
师德标兵先进事迹材料
2014/12/19 职场文书
师范生见习自我总结
2015/06/23 职场文书
80后创业总结的9条职场用人思想,记得收藏
2019/08/13 职场文书
关于Javascript闭包与应用的详解
2021/04/22 Javascript
golang 实现Location跳转方式
2021/05/02 Golang
SQL实现LeetCode(196.删除重复邮箱)
2021/08/07 MySQL
Java面试题冲刺第十八天--Spring框架3
2021/08/07 面试题