Jimmy Adaro
Jimmy Adaro

Reputation: 1405

Check if elapsed 5 minutes from PHP time stamp

I've done some research but nothing useful come out.

I need to check if a DB time stamp is -5 minutes from current time stamp. That time stamp is created with PHP time() function.

I tried with this without any luck:

$timestamp = "1451999400";

if(strtotime($timestamp) > strtotime("-5 minutes")) {
    echo "A";
} else {
    echo "B";
}

That code always returns "B".

Upvotes: 3

Views: 1795

Answers (1)

Crouching Kitten
Crouching Kitten

Reputation: 1185

First make sure, that both the database and the PHP are using the same time zone. Then:

$timestamp = "1451999400";

if($timestamp > time() - 60*5) {
    echo "A";
} else {
    echo "B";
}

You don't need to convert the string to integer, because PHP will auto-convert it to a numeric type (possibly integer). (Because of the > operator)

Upvotes: 2

Related Questions