将数据库中的时间戳与当前时间进行比较

php
2022-08-30 15:11:08

我需要根据数据库中的日期时间来测试当前时间,如果它已经是30分钟,那么执行代码,如果没有,那就不要。这就是我所处的位置,我被困住了:

$link = mysqli_connect("hostname", "username", "password", "database");
$q = "SELECT id FROM dwCache ORDER BY id DESC LIMIT 1";
$qu = mysqli_query($link, $q);

while($row=mysqli_fetch_array($qu, MYSQL_ASSOC)){
        $id = $row['id'];
        $cache = $row['cache'];
        $timest = $row['time'];
    }

$newTime = 
$difference = $timest
if($timest >= )

正如你所看到的,在底部,我失去了它,因为我不知道该怎么办。

$timest返回 :作为格式2013-02-01 12:36:01Y-m-d h-i-s

对双发道歉,其他删除。


答案 1

首次转换为时间戳$timest

$time = strtotime($timest);

$curtime = time();

if(($curtime-$time) > 1800) {     //1800 seconds
  //do stuff
}

答案 2

在 sql 语句中完成所有操作

SELECT id FROM `dqCache` WHERE `time`<DATE_SUB(NOW(), INTERVAL 30 MINUTE);

这将返回表中时间列在 30 分钟之前的所有内容。


推荐