如果自时间戳以来已超过 3 天,请执行操作


If it has gone more than 3 days since timestamp, then do action

我在$user_timestamp中存储了一个特定的时间戳,例如可以是1421942631 .我将当前时间戳存储在$current_timestamp.

我想将此变量中的时间戳与当前时间戳进行比较,并检查它是否已过去 3 天。如果有,那就采取行动。

如何在基本if语句中执行此操作?

虽然我通常建议使用 DateTime 进行日期数学运算,但您可以只使用带有 strtotime() 的相对格式来保持简单。

if($user_timestamp < strtotime('-3 days')) {
     // three days ago or more
}

但为了完整起见,这里是日期时间示例。

$userTimestamp = new DateTime('@'.$user_timestamp);
$threeDaysAgo  = new DateTime('-3 days');
if ($userTimestamp < $threeDaysAgo) {
     // three days ago or more
}

无需转换它或使用其他函数。 3 天 = 259200 秒,所以这应该有效:

if( $user_timestamp < ( $current_timestamp-259200 ) ){