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

Php 如果自上次更新后现在的时间超过3小时,php,Php,我需要进行验证,以检查“上次更新”是否在3小时或更短时间内完成 我有以下PHP代码: if($user['last_update'] < strtotime("3 hours")) { $msg .= "You can only register every 3 hour."; } if($user['last_update']

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

我有以下PHP代码:

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

这是怎么做到的?

像这样试试。将您的
$user['last_update']
包装在
strotime()中

if(strtotime($user['last_update']) < strtotime("3 hours")) {
    $msg .= "You can only register every 3 hour.";
}
if(strotime($user['last_update'])
像这样试试。将您的
$user['last_update']
包装在
strotime()中

if(strtotime($user['last_update']) < strtotime("3 hours")) {
    $msg .= "You can only register every 3 hour.";
}
if(strotime($user['last_update'])
如果
$user['last\u update']
为日期格式,则

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

如果
$user['last\u update']
为日期格式,则

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

您可以使用此选项获得当前时间减去3小时:

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

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

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

您可以使用此选项获得当前时间减去3小时:

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

$time = $dateTime->format("H:m:s");
$date = $dateTime->format("Y-m-d");
然后,您可以将日期和时间变量与上次更新进行比较。

strotime()

返回秒数

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

$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.";
}
$three_hours=日期('Y-m-d H:i:s',标准时间('3小时');
如果($user['last\u update']<$three\u hours){
$msg.=“您只能每3小时注册一次。”;
}
strotime()

返回秒数

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

$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.";
}
$three_hours=日期('Y-m-d H:i:s',标准时间('3小时');
如果($user['last\u update']<$three\u hours){
$msg.=“您只能每3小时注册一次。”;
}

user['last\u update']
的格式是什么?
user['last\u update']
的格式是什么?事实上,如果是可识别的日期格式,它就会工作。谢谢!它工作得很好!$user['last_update']处于unix时间,因此我刚刚删除了strotime(),再次感谢!事实上,如果使用可识别的日期格式,就可以了。谢谢!它工作得很好!$user['last_update']处于unix时间,因此我刚刚删除了strotime(),再次感谢!您是指字符串比较吗?@shiplu.mokadd.im否,它们是php日期对象,php时间对象
$date
$time
是字符串。如果您想让OP将它们与其他变量进行比较,那么您不是在用
进行字符串比较吗?@shiplu.mokadd.im否,它们是php日期对象,php时间对象
$date
$time
是字符串。如果您想让OP将它们与其他变量进行比较,您是不是指使用