0

我有一个特定的时间戳存储在$user_timestamp,例如1421942631。我将当前时间戳存储在$current_timestamp.

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

我怎样才能在基本if语句中做到这一点?

4

2 回答 2

1

虽然我通常建议使用 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
}
于 2015-01-22T16:10:35.210 回答
1

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

if( $user_timestamp < ( $current_timestamp-259200 ) ){
于 2015-01-22T16:19:42.367 回答