Php 以分钟和秒为单位计算时间差

Php 以分钟和秒为单位计算时间差,php,datetime,Php,Datetime,我尝试比较2个datetime,以分钟和秒为单位得出不同的结果,在我参考本主题之后,是的,代码可以以分钟为单位显示不同的结果: $to_time = strtotime("2008-12-13 18:42:00"); $from_time = strtotime("2008-12-13 18:41:58"); echo round(abs($to_time - $from_time) / 60,2). " minute"; 那么如何从上述代码中以分和秒的形式显示?我的php版本是5.2.17

我尝试比较2个datetime,以分钟和秒为单位得出不同的结果,在我参考本主题之后,是的,代码可以以分钟为单位显示不同的结果:

$to_time = strtotime("2008-12-13 18:42:00");
$from_time = strtotime("2008-12-13 18:41:58");

echo round(abs($to_time - $from_time) / 60,2). " minute";
那么如何从上述代码中以分和秒的形式显示?我的php版本是
5.2.17

或者使用for php>=5.3:-

$minutes = round(abs($to_time - $from_time) / 60,2);
$seconds = abs($to_time - $from_time) % 60;

echo "$minutes minute, $seconds seconds";
$to_time = new \DateTime('2008-12-13 18:42:00');
$from_time = new \DateTime('2008-12-13 18:41:58');
$diff = $from_time->diff($to_time);
echo $diff->format('%i Minutes %s Seconds');
注意:`$diff'将是的一个实例

或者,稍微简洁一些,但可读性较差:-

$to_time = new \DateTime('2008-12-13 18:42:00');
$from_time = new \DateTime('2008-12-13 18:41:58');

echo $to_time->diff($from_time)->format('%i Minutes %s Seconds');