如何检测时间戳是否超过一定时间

时间:2012-02-24 02:51:34

标签: php

我按照常规方式格式化时间戳:

2012-02-23 20:34:55

我需要检测以下内容。

if(Timestamp >= 1 month ago) {

}

if(Timestamp >= 5 minutes ago) {

}

您如何从PHP中的时间戳变量中检测到该类型的事物?

>=我的意思是时间戳至少或超过5分钟前​​。

2 个答案:

答案 0 :(得分:5)

if (strtotime($timestamp) <= strtotime('-1 month'))

答案 1 :(得分:3)

首先,从时间字符串创建一个DateTime对象,现在创建一个

$dt = new DateTime($timestamp);
$now = new DateTime;

然后,创建一些DateInterval对象

$oneMonth = new DateInterval('P1M');
$fiveMinutes = new DateInterval('PT5M');

然后,进行比较

if ($dt >= $now->sub($oneMonth)) {
}

if ($dt >= $now->sub($fiveMinutes)) {
}