这篇文章主要介绍了php获取给定日期相差天数的方法,结合具体实例形式分析了2种日期相差天数的计算方法,涉及php日期字符串转换的相关操作技巧,需要的朋友可以参考下
本文实例讲述了php获取给定日期相差天数的方法。分享给大家供大家参考,具体如下:
方法一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?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);
}
$date1 = strtotime ( date ( "Y-m-d" ));
$date2 = strtotime ('2017-8-26');
$result =count_days( $date1 , $date2 );
echo $result ;
?>
|
Copier après la connexion
运行结果:187
方法二:
1 2 3 4 5 6 7 8 9 | <?php
$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 . "天" ;
?>
|
Copier après la connexion
运行结果:
Copier après la connexion
PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:
在线日期/天数计算器:
http://www.php.cn/
在线日期计算器/相差天数计算器:
http://www.php.cn/
在线日期天数差计算器:
http://www.php.cn/
Unix时间戳(timestamp)转换工具:
http://www.php.cn/