Php 如何比较datetime字段之间的运行时间?
mySql中有两个datetime类型的字段,如下所示:Php 如何比较datetime字段之间的运行时间?,php,mysql,Php,Mysql,mySql中有两个datetime类型的字段,如下所示:2009-07-26 18:42:21。在PHP中检索到这些值后,如何比较这两个时间戳以计算它们之间经过了多少秒?我试着简单地减去它们,但没有成功。选择它们作为unix时间戳,然后从另一个时间戳中减去一个时间戳 $ts1 = strtotime('2009-07-26 18:42:21'); $ts2 = strtotime('2009-07-26 18:42:20'); $elapsedSeconds = $ts1 - $ts2; //
2009-07-26 18:42:21
。在PHP中检索到这些值后,如何比较这两个时间戳以计算它们之间经过了多少秒?我试着简单地减去它们,但没有成功。选择它们作为unix时间戳,然后从另一个时间戳中减去一个时间戳
$ts1 = strtotime('2009-07-26 18:42:21');
$ts2 = strtotime('2009-07-26 18:42:20');
$elapsedSeconds = $ts1 - $ts2; // = 1
SELECT UNIX_TIMESTAMP(field1) AS time1, UNIX_TIMESTAMP(field2) AS time2
选择它们作为unix时间戳,然后从另一个时间戳中减去一个时间戳
SELECT UNIX_TIMESTAMP(field1) AS time1, UNIX_TIMESTAMP(field2) AS time2
试试这个PHP代码来比较日期
<?php
$date_difference_in_seconds = abs(strtotime($date_1) - strtotime($date_2));
?>
试试这个PHP代码来比较日期
<?php
$date_difference_in_seconds = abs(strtotime($date_1) - strtotime($date_2));
?>