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怎么计算时间相减差距几天

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。