Heim  >  Artikel  >  php教程  >  php获取给定日期相差天数的方法分析_php技巧

php获取给定日期相差天数的方法分析_php技巧

PHP中文网
PHP中文网Original
2016-05-25 17:11:21914Durchsuche

这篇文章主要介绍了php获取给定日期相差天数的方法,结合具体实例形式分析了2种日期相差天数的计算方法,涉及php日期字符串转换的相关操作技巧,需要的朋友可以参考下

本文实例讲述了php获取给定日期相差天数的方法。分享给大家供大家参考,具体如下:

方法一:

<?php
function count_days($a,$b){
 $a_dt=getdate($a);
 $b_dt=getdate($b);
 $a_new=mktime(12,0,0,$a_dt[&#39;mon&#39;],$a_dt[&#39;mday&#39;],$a_dt[&#39;year&#39;]);
 $b_new=mktime(12,0,0,$b_dt[&#39;mon&#39;],$b_dt[&#39;mday&#39;],$b_dt[&#39;year&#39;]);
 return round(abs($a_new-$b_new)/86400);
}
//今天与2017年8月26日相差多少天
$date1=strtotime(date("Y-m-d"));
$date2=strtotime(&#39;2017-8-26&#39;);
$result=count_days($date1,$date2);
echo $result;
?>

运行结果:187

方法二:

<?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."天";
?>

运行结果:

今天与2017年8月26日相差187天

PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

在线日期/天数计算器:
http://www.php.cn/

在线日期计算器/相差天数计算器:
http://www.php.cn/

在线日期天数差计算器:
http://www.php.cn/

Unix时间戳(timestamp)转换工具:
http://www.php.cn/

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP监控服务器【LNMPA】Nächster Artikel:php 可逆加密的方法