贝利信息

php时间怎么计算_strtotime函数计算时间差的应用【方法】

日期:2025-12-31 00:00 / 作者:看不見的法師
strtotime不计算时间差,仅转换字符串为时间戳;直接相减再除86400易因时区、夏令时等出错;正确做法是用DateTime::diff获取DateInterval对象。

strtotime 计算时间差容易出错的根本原因

strtotime 本身不计算时间差,它只把字符串转成时间戳(int)。很多人误以strtotime('2025-03-10') - strtotime('2025-03-01') 就是“9天”,但结果其实是秒数(777600),直接除以 86400 才得天数——而这个过程在时区切换、夏令时、闰秒等场景下会悄悄出错。

正确用法:先转时间戳,再用 date_diff 或手动换算

PHP 7.0+ 推荐用 DateTime 对象配合 diff() 方法,避免手动算秒数。如果必须用 strtotime,只用于生成时间戳,后续逻辑交给 DateInterval 处理:

$start = new DateTime('@' . strtotime('2025-03-01'));
$end   = new DateTime('@' . strtotime('2025-03-10'));
$diff  = $start->diff($end);
echo $diff->days; // 输出 9

strtotime 参数陷阱:相对时间表达不等于绝对时间差

strtotime('+7 days', $timestamp) 是“从某时刻起加 7 天”,不是“计算和现在相差 7 天”。它受当前时区、DST 影响,且不能反向推导差值:

性能与兼容性提醒

频繁调用 strtotime 解析相同格式字符串(如循环中解析 'Y-m-d')效率低。可提前缓存或改用 DateTime::createFromFormat

$date = DateTime::createFromFormat('Y-m-d', '2025-03-01');

真正需要“时间差”的地方,DateTime::diff 返回的 DateInterval 对象才是唯一可靠来源;strtotime 只该出现在初始化阶段,不该参与差值逻辑。