PHP-检查哈希是否已过期


PHP - Check if hash has expired

我想检查我存储在数据库中的哈希是否过期,即超过30分钟。

这是我的支票

$db_timestamp = strtotime($hash->created_at);
$cur_timestamp = strtotime(date('Y-m-d H:i:s'));
if (($cur_timestamp - $db_timestamp) > ???) {
    // hash has expired
}

时间戳是秒数,因此您希望查看哈希的年龄(以秒为单位)是否大于30分钟内的秒数。

算出30分钟内有多少秒:

$thirtymins = 60 * 30;

然后使用这个字符串:

if (($cur_timestamp - $db_timestamp) > $thirtymins) {
    // hash has expired
}

您还可以通过执行以下操作来清理代码:

$db_timestamp = strtotime($hash->created_at);
if ((time() - $db_timestamp) > (30 * 60)) {
    // hash has expired
}