Php 如何使用strotime检查时间是否超过一定的时间?

Php 如何使用strotime检查时间是否超过一定的时间?,php,unix,Php,Unix,我想知道我如何检查时间是否在特定的分钟数之后,如果超过两小时,我想知道如何检查时间是否在特定的分钟数之后。。我尝试了以下方法,但似乎不起作用 $postTime是unix时间 if(strtotime("+20 minutes") > strtotime($postTime)) { echo 'REFRESH EVERY 20 minutes'; } else if(strtotime("+2 hours")> strtotime($postTime)) { ech

我想知道我如何检查时间是否在特定的分钟数之后,如果超过两小时,我想知道如何检查时间是否在特定的分钟数之后。。我尝试了以下方法,但似乎不起作用

$postTime是unix时间

if(strtotime("+20 minutes") > strtotime($postTime))
{
    echo 'REFRESH EVERY 20 minutes';

} else if(strtotime("+2 hours")> strtotime($postTime))
{
    echo 'refresh every 2 hours';
}
有没有更有效的方法来检查它是否超过了特定的分钟/小时数,或者strotime是最好的方法

编辑:


否则,如果与postTime相比,时间已超过2小时,则除非$postTime是未来超过20分钟的时间,否则您的第一条语句将始终匹配,您不会检查$postTime是否在20分钟之前

strotime不是最有效的方法,最有效的方法就是对整数进行算术运算。php的Time函数(包括Time和strotime)返回一个unix时间戳整数

最简单的方法是:

// If $postTime is a unix timestamp integer
if ( $postTime < (time()-(60*20)) ) {
...
}

// The above statement is equivalent to:
if ( $postTime < strtotime("-20 minutes") ) {
...
}

// If $postTime is a string
if ( strtotime($postTime) < (time()-(60*20)) ) {

}

我们可以使用JavaScript函数来实现setInterval@kannan因为他没有使用javascript。这是一种检查时间间隔的好方法,它的效率与你所需要的一样。我编辑了这个问题,因为我仍然无法理解为什么它不能按当时的方式工作。@iBrazilian2,我已经根据你的编辑编辑了我的答案。您正在对照未来的时间进行检查,而不是检查$postTime是否在过去是x分钟。
// If $postTime is a unix timestamp integer
if ( $postTime < (time()-(60*20)) ) {
...
}

// The above statement is equivalent to:
if ( $postTime < strtotime("-20 minutes") ) {
...
}

// If $postTime is a string
if ( strtotime($postTime) < (time()-(60*20)) ) {

}