【发布时间】:2015-01-22 16:09:24
【问题描述】:
我在$user_timestamp 中存储了一个特定的时间戳,例如1421942631。我将当前时间戳存储在$current_timestamp 中。
我想将此变量中的时间戳与当前时间戳进行比较,并检查它是否已经过去了 3 天。如果有,请采取行动。
如何在基本的if 语句中做到这一点?
【问题讨论】:
标签: php date timestamp date-math
我在$user_timestamp 中存储了一个特定的时间戳,例如1421942631。我将当前时间戳存储在$current_timestamp 中。
我想将此变量中的时间戳与当前时间戳进行比较,并检查它是否已经过去了 3 天。如果有,请采取行动。
如何在基本的if 语句中做到这一点?
【问题讨论】:
标签: php date timestamp date-math
虽然我通常建议使用 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 ) ){
【讨论】:
// 259200 = 3 days 的行中添加评论