使用php进行Mysql数据库时间戳查询

时间:2015-01-23 17:13:38

标签: php mysql

使用php的Mysql数据库时间戳查询

我在数据库中插入了一个时间戳值,设置为3小时,如下所示

//For 3 hours ahead of time
$reciever_time = date("Y-m-d H:i:s", strtotime('+3 hours'));

现在一旦达到3小时,我需要回应" 3小时到期"但是下面的代码仍然响应"它还没有3个小时"即使  3个小时到期。任何帮助

<?php


$link = mysqli_connect("localhost", "sectona", "secBBN", "sectonadb");
$q = "SELECT id,reciever_time FROM indextime WHERE id='10'";
$qu = mysqli_query($link, $q);

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

    }

//60secx10min is 600milisec
//60secx30min is 1800
// 30 min = 1800
// 1 hour = 1800 x2
//12 hour = 3600 x12
//1 day = 3600 x24


$time = strtotime($timest);
$curtime = time();




// execute data that is due in 3 hours
if(($curtime-$time) >= 10800) {
  print "3 hours is due then update";
}


else{

  print "its not yet 3 hours";
}


?> 

3 个答案:

答案 0 :(得分:1)

试试这个:

if(($time - $curtime) >= -10800) {
  print "3 hours is due then update";
} else {
  print "its not yet 3 hours";
}

仅将10800转换为否定。

答案 1 :(得分:0)

在PHP中,您希望使用date_diff函数来获取时间差异 尝试:

if(date_diff($time,$curtime) >= 10800){

答案 2 :(得分:0)

您可以尝试:

$link = mysqli_connect("host","user","password","DBname");

$q = "SELECT id,reciever_time FROM indextime WHERE id='10' LIMIT 1";
$qu = $link->query($q);
$row = mysqli_fetch_object($qu);
$id = $row->id;
$timest = $row->reciever_time; 
$time = strtotime($timest);
$curtime = time();
$message = ($curtime - $time) >= 10800 ? "3 hours is due then update" : "its not yet 3 hours";
print $message;