PHP计算两个时间相差的年数、月数和天数程序
计算时间差我们原理是根据定义1、一年为360天,一个月为30天;2、代码中86400=24*60*60,代表一天中共有多少秒,这样就可以计算出来了
效果如下

<?php
//Get detail gap of year,month and days between two different time by vfhky 20130728
$common = (time()-strtotime(get_option('swt_builddate')));
$a = floor($common/86400/360); //整数年
$b = floor($common/86400/30) - $a*12; //整数月
$c = floor($common/86400) - $a*360 - $b*30; //整数日
$d = floor($common/86400); //总的天数
echo $a."年".$b."月".$c."日(共计".$d."天)";
?>其它的一些方法
<?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);
}
//今天与2008年10月11日相差多少天
$date1=strtotime(time());
$date1=strtotime('10/11/2008');
$result=count_days($date1,$date2);
echo $result;
?>例2
<?php
//今天与2008年9月9日相差多少天
$Date_1=date("Y-m-d");
$Date_2="2008-10-11";
$d1=strtotime($Date_1);
$d2=strtotime($Date_2);
$Days=round(($d2-$d1)/3600/24);
echo "今天与2008年10月11日相差".$Days."天";
?>总结
从上面实例我们可以看得出来其实就是使用mktime与strtotime了,然后再通过计算出来的时间进行加减就得出来我们要的时间日期了。
文章链接:http://www.phprm.com/develop/55548.html
随便收藏,请保留本文地址!