Mysql 子时间当前时间 - $row['time']

Mysql subtime currenttime - $row['time']

我正在构建一个简单的网页。在我第一次将当前时间存储在数据库中时加载页面。再次加载页面时,我想获取自第一次加载以来已经过去的时间。

我发现我可能必须使用 SUBTIME 来实现这一点。所以基本上它会是这样的:

$result = mysql_query("SELECT SUBTIME('now()','time')"); // in this case time is the name of the database row.

我在网上四处寻找,但找不到解释如何执行此操作的示例。希望有人能帮帮我。

您好!

如果 'time' 是您的数据库列的名称,请使用反引号而不是引号来获取值:

$result = mysql_query("SELECT SUBTIME(NOW(),`time`) AS `total_time`");

NOW() 不应在引号中。

我不会为此使用 MySQL。

从数据库中检索时间

$starttime = strtotime($row['time']); // hopefully a datetime value
$endtime = time();
$totaltime = $endtime - $starttime; // gives the time in seconds

然后将其写回您的数据库table,或者您想对该信息执行的任何操作