Mysql subtime currenttime - $ row [' time']

时间:2015-03-25 21:12:25

标签: php mysql time

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

我发现我可能不得不使用SUBTIME来实现这一目标。基本上它会是这样的:

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

我在网上环顾四周,但找不到一个解释我如何做到这一点的例子。希望有人能帮助我。

问候!

2 个答案:

答案 0 :(得分:1)

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

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

NOW()不应该在引号中。

答案 1 :(得分:0)

我不会使用MySQL。

从数据库中检索时间

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

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