php怎么算两个时间中差几天

php怎么算两个时间中差几天插图

本文操作环境:Windows10系统、php7.1版、Dell G3电脑

1、利用strtotime函数将时间格式化为时间戳

strtotime() 函数将任何英文文本的日期或时间描述解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数)。

语法为:

strtotime('年-月-日 时:分:秒')

示例如下:

<?php 
echo(strtotime('2022-04-13 12:25:00')); 
?>

输出结果:

php怎么算两个时间中差几天插图1

2、将两个时间戳相减,获取两个时间的时间差

3、将获取的时间差除以86400就是两个时间相差的天数

示例如下:

<?php 
$time1=strtotime('2022-04-13 12:25:00'); 
$time2=strtotime('2022-04-15 12:25:00'); 
$diff_seconds = $time2 - $time1;
$diff_days = $diff_seconds/86400;
echo($diff_days . "天");
?>

输出结果:

php怎么算两个时间中差几天插图2

THE END