如何从数据库中的日期时间中减去当前日期时间-PHP
我正在一个项目上.我必须检查多少分钟前,用户更新了数据库".为此,我使用了以下代码:
I'm working on a project.I have to check " how many minutes ago ,user updated the database " .For that I used following code :
<?php
include('connect_db.php');
$sql =" SELECT * FROM test_table WHERE user='john' ORDER BY time_ DESC LIMIT 1" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$time = strtotime($row['time_']);
$current=time();
$sub_time=$current-$time;
echo $sub_time;
}
问题在于代码正在返回负值,例如[-17173]. 存储在我的数据库中的日期时间就像[2018-07-14 11:42:21.006515]
The problem is the code is returning negative values like [ -17173 ]. The date-time stored in my db is like [ 2018-07-14 11:42:21.006515 ]
我只是想将当前时间与数据库中存储的时间进行比较,以秒或分钟为单位. 我需要帮助解决此问题.谢谢.
I simply want to compare current time with the the time stored in database get results in seconds or minutes . I need help to solve this issue.Thank you .
您可以更改select语句,以秒为单位提供时差,如下所示:
You can just change your select statement to give you the time difference in seconds as shown below:
$sql =" select UNIX_TIMESTAMP(current_Timestamp()) - UNIX_TIMESTAMP(time_) FROM test_table WHERE user='john' ORDER BY time_ DESC LIMIT 1" ;