ltdev
ltdev

Reputation: 4497

PHP - Check if hash has expired

I want to check if a hash I've stored in my database is expired, i.e older than 30 minutes.

This is my check

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

Upvotes: 0

Views: 275

Answers (1)

Ben
Ben

Reputation: 9001

Timestamps are numbers of seconds, so you want to see if the age of the hash (in seconds) is greater than the number of seconds in 30 minutes.

Figure out how many seconds there are in 30 minutes:

$thirtymins = 60 * 30;

then use that string:

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

You can also cleanup the code by doing the following:

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

Upvotes: 3

Related Questions