如果现在时间超过上次更新后的3小时

时间:2013-09-24 10:53:34

标签: php

我需要进行验证,检查“last_update”是否在3小时或更短时间内完成。

我有这个PHP代码:

if($user['last_update'] < strtotime("3 hours")) {
    $msg .= "You can only register every 3 hour.";
}

怎么可以这样做?

4 个答案:

答案 0 :(得分:3)

如果$user['last_update']采用日期格式,则

if (time() - strtotime($user['last_update']) < 3 * 3600) {
  //..
}

答案 1 :(得分:1)

试试这样。将您的 $user['last_update'] 包含在 strtotime() 中。

if(strtotime($user['last_update']) < strtotime("3 hours")) {
    $msg .= "You can only register every 3 hour.";
}

答案 2 :(得分:1)

您可以使用此功能将当前时间减去3小时:

$dateTime = new DateTime();
$dateTime->modify("-3 hours");

$time = $dateTime->format("H:m:s");
$date = $dateTime->format("Y-m-d");

然后,您可以将日期和时间变量与上次更新进行比较。

答案 3 :(得分:0)

strtotime()

返回秒。

如果您的日期存储为UNIX时间戳,那么您的代码是正确的。 如果您的日期存储为TIMESTAMP,则以下代码为:

$three_hours = date('Y-m-d H:i:s', strtotime('3 hours'));
if ($user['last_update'] < $three_hours) {
    $msg .= "You can only register every 3 hour.";
}
相关问题