如何从数据库中的日期时间中减去当前日期时间-PHP [英] How to Subtract current date time from date time in database - PHP

查看:730
本文介绍了如何从数据库中的日期时间中减去当前日期时间-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" ; 

这篇关于如何从数据库中的日期时间中减去当前日期时间-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆