本文实例讲述了php获取给定日期相差天数的方法。分享给大家供大家参考,具体如下:
[b]方法一:
[/b]
<?php
function count_days($a,$b){
$a_dt=getdate($a);
$b_dt=getdate($b);
$a_new=mktime(12,0,0,$a_dt['mon'],$a_dt['mday'],$a_dt['year']);
$b_new=mktime(12,0,0,$b_dt['mon'],$b_dt['mday'],$b_dt['year']);
return round(abs($a_new-$b_new)/86400);
}
//今天与2017年8月26日相差多少天
$date1=strtotime(date("Y-m-d"));
$date2=strtotime('2017-8-26');
$result=count_days($date1,$date2);
echo $result;
?>
运行结果:187
[b]方法二:[/b]
<?php
//今天与2017年8月26日相差多少天
$Date_1=date("Y-m-d");
$Date_2="2017-8-26";
$d1=strtotime($Date_1);
$d2=strtotime($Date_2);
$Days=round(($d2-$d1)/3600/24);
echo "今天与2017年8月26日相差".$Days."天";
?>
运行结果:
[b]PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:[/b]
[b]在线日期/天数计算器:
[/b][url=http://tools.jb51.net/jisuanqi/date_jisuanqi]http://tools.jb51.net/jisuanqi/date_jisuanqi[/url]
[b]在线日期计算器/相差天数计算器:
[/b][url=http://tools.jb51.net/jisuanqi/datecalc]http://tools.jb51.net/jisuanqi/datecalc[/url]
[b]在线日期天数差计算器:
[/b][url=http://tools.jb51.net/jisuanqi/onlinedatejsq]http://tools.jb51.net/jisuanqi/onlinedatejsq[/url]
[b]Unix时间戳(timestamp)转换工具:
[/b][url=http://tools.jb51.net/code/unixtime]http://tools.jb51.net/code/unixtime[/url]
更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/96.htm]php日期与时间用法总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》、《[url=http://www.1sucai.cn/Special/348.htm]PHP基本语法入门教程[/url]》、《[url=http://www.1sucai.cn/Special/357.htm]PHP运算与运算符用法总结[/url]》、《[url=http://www.1sucai.cn/Special/43.htm]php面向对象程序设计入门教程[/url]》、《[url=http://www.1sucai.cn/Special/495.htm]PHP网络编程技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/84.htm]php+mysql数据库操作入门教程[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》
希望本文所述对大家PHP程序设计有所帮助。