开发者

Comparing timestamps in PHP

开发者 https://www.devze.com 2023-04-06 01:48 出处:网络
How would I be 开发者_如何学Cable to return a boolean expression of true once exactly 24 hours have passed and no more?

How would I be 开发者_如何学Cable to return a boolean expression of true once exactly 24 hours have passed and no more?

I am using strtotime() to out put these two times:

$time1 = 1316268919;
$time2 = 1316268898;


This should do what you need:

if (($time1-$time2) == 86400) {
    // Exactly 24 hours have passed.
}


strtotime() returns the number of seconds since the Unix epoch. So to find the difference in times, simply subtract one from another. Then compare that difference against the number of seconds in 24 hours (24 * 60 * 60).


abs($time1 - $time2) === 60 * 60 * 24

the abs is just in case; time2 might be bigger than time1.

If the difference in times equals 60 (seconds in a minute) * 60 * (minutes per hour) * 24 (hours for a day) = 86400, there's a day difference


24h=86400sec so, when difference of two is 86400, it's exactly 24h

n.b. twice a year could cause issue do to DST, if applicable.

0

精彩评论

暂无评论...
验证码 换一张
取 消