Check if timestamp is x hours old?

tctc91 picture tctc91 · Jan 7, 2012 · Viewed 17.3k times · Source

Simple question, but I couldn't find any related topics that match this exact scenario. Everything I found involved MySQL or date and time rather than just time.

I have a timestamp stored in a variable in the format of:

1325960262

Produced by:

$newTimeStamp = time();

How can I check if this timestamp is older than 2 hours?

I tried:

if (strtotime($ratesTimeStamp) <= strtotime('-2 hours')) {
    echo "OLDER";
} else {
    echo "not older";
}

But no luck.

Answer

Tim Cooper picture Tim Cooper · Jan 7, 2012

Drop the first strtotime; the variable is already in timestamp form:

if ($ratesTimeStamp <= strtotime('-2 hours')) {
    echo "OLDER";
} else {
    echo "not older";
}