PHP检查是否已经过了10分钟

时间:2017-03-26 20:19:32

标签: php mysql

我想比较两个dates,看看是否已经过了10分钟。

这是我已经获得的代码,但我无法弄明白该怎么做。

我从我的表中获取第一个日期(保存为timestamp,例如:2017-03-26 22:33:45)然后我想将其与time进行比较这是正确的now

$sql = "SELECT saved_time from table1 where email = '$email'";
$stmt = $conn->prepare($sql);
$stmt->execute();
while($row = $stmt->fetch()) {
    $savedTime = $row[0];
}
$now = time();
if (/*10 minutes has passed between saved time and now*/) {
    echo "Your account is unlocked";
} else if (/*10 minutes hasn't passed*/) {
    echo "Your account is locked";
}

3 个答案:

答案 0 :(得分:3)

尝试此代码:

    $timezone = "Asia/Kolkata";// Select Timezone as of your Preference or MySQL Server Timezone
    date_default_timezone_set($timezone);
    $sql = "SELECT saved_time from table1 where email = :email";
    $stmt = $conn->prepare($sql);
    $stmt->execute(array(':email' => $email));
    while($row = $stmt->fetch()) {
        $savedTime = $row[0];
    }

    // Uncomment below Line if $savedTime is in MySQL DATETIME Format
    // $savedTime = strtotime($savedTime);
    $now = time();

    if (round(($now - $savedTime) / 60,2) >= 10){
       echo "Your account is unlocked";
    } elseif (round(($now - $savedTime) / 60,2) < 10){
       echo "Your account is locked";
    }

答案 1 :(得分:0)

if $now = time(); #(this will generate a timestamp)

假设$savedTime = $row[0];是MySQL日期字段,您可以

$savedTimeTimestamp= date("Y-m-d H:i:s", $savedTime);

...现在10次60秒是10分钟,所以

if ($now > ($savedTimeTimestamp+600))

应该做的工作

答案 2 :(得分:0)

如果mysql解决方案适合你,那么

select * from table T where TIMESTAMPDIFF(MINUTE,.saved_time,NOW()) > 10 

如果你需要在php中,那么

$db_time = $row['saved_time'];//as you have stored in timestamp already
$curr_time = strtotime(date('Y-m-d H:i:s'));
$diff =  round(abs($db_time - $curr_time) / 60,2);
if($diff > 10)

<强>更新

select now() as curr_time, saved_time from table where COND;

然后在php

$diff = $curr_time-$saved_time;
if($diff > (10*60 ))

我不确定这是否有效。但只是一个想法。