如果时间戳后超过 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
}

但为了完整起见,这里是 DateTime 示例。

$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 ) ){