php中两个时间如何相减

如题所述

PHP 中的 strtotime() 函数可以实现

strtotime() 函数将任何英文文本的日期时间描述解析为 Unix 时间戳。

strtotime(time,now)

参数说明
time 规定要解析的时间字符串。
now 用来计算返回值的时间戳。如果省略该参数,则使用当前时间。

详细说明
成功则返回时间戳,否则返回 FALSE。在 PHP 5.1.0 之前本函数在失败时返回 -1。

例如:
<?php
$start_time = '2015-05-01 10:10:10';
$end_time = '2015-06-01 10:10:10';

//下面计算出的是秒,可以转化为天、时、分等
echo strtotime($end_time )-strtotime($start_time);
?>
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-08-20
//可以先将时间转化为时间戳进行加减,然后在转化为时间
$a = date('H:i:s');
$b = date('H:i:s',strtotime('+50 second'));
$aa = strtotime($b)-strtotime($a);
echo date('H:i:s', $aa);

本回答被提问者和网友采纳