Check if timestamp is x hours old?

女生的网名这么多〃 提交于 2019-11-29 17:00:46

问题


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.


回答1:


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

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



回答2:


$testedTime = 1325960262;
$currentTime = time();

if($currentTime - $testedTime > 7200){
    /// Older than 2 hours
}


来源:https://stackoverflow.com/questions/8771981/check-if-timestamp-is-x-hours-old

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!