【发布时间】:2018-07-14 08:04:25
【问题描述】:
我正在做一个项目。我必须检查“几分钟前,用户更新了数据库”。为此我使用了以下代码:
<?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]
我只是想将当前时间与存储在数据库中的时间进行比较,以秒或分钟为单位获得结果。 我需要帮助来解决这个问题。谢谢。
【问题讨论】:
-
您的代码看起来不错,但我不确定
strtotime()能否理解您的日期时间格式。 PHP 手册不是很有帮助:php.net/manual/en/datetime.formats.compound.php 我在那里看不到你的。为什么不剪掉最后一点,使用 MySQL 格式呢?所以你只剩下这一点:2018-07-14 11:42:21. -
time() 函数以 UTC 格式返回日期。如果您的数据库时间是本地时间,请改用 date()。