Check if timestamp is x hours old?
Asked Answered
M

2

20

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.

Mandler answered 7/1, 2012 at 18:28 Comment(0)
O
35

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

if ($ratesTimeStamp <= strtotime('-2 hours')) {
    echo "OLDER";
} else {
    echo "not older";
}
Outspoken answered 7/1, 2012 at 18:29 Comment(0)
A
9
$testedTime = 1325960262;
$currentTime = time();

if($currentTime - $testedTime > 7200){
    /// Older than 2 hours
}
Agamete answered 7/1, 2012 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.