站长资讯网
最全最丰富的资讯网站

php怎么计算时间相减差距几天

php计算时间相减差距几天的方法:1、使用“strtotime("时间")”语句将两个时间转为时间戳;2、将两个时间戳相减,得到时间差;3、将时间差除以一天的总秒数,并用floor()取整即可,语法“floor(时间差/86400)”。

php怎么计算时间相减差距几天

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

php怎么计算时间相减差距几天

实现思想:

  • 使用strtotime()函数将两个时间转为时间戳

  • 将两个时间戳相减,得到时间差

  • 将时间差除以一天的总秒数(24*60*60=86400)

  • 用floor()取整

实现代码:

<?php header("Content-type:text/html;charset=utf-8"); //2022年1月1日 19点30分0秒 $time1=strtotime("2022-1-1"); //2022年7月7日 7点30分0秒 $time2=strtotime("2022-7-7");  $diff_seconds = $time2 - $time1; $diff_days = floor($diff_seconds/86400); echo "两个时间相差: ".$diff_days." 天"; ?>

php怎么计算时间相减差距几天

推荐学习:《PHP视频教程》

赞(0)
分享到: 更多 (0)