比较Unix时间戳

seh*_*mel 0

我需要比较两个Unix时间戳,我在数学上遇到了麻烦.我知道Unix时间戳是自1970年1月1日以来的秒数.但是我的数学运算出错了.我正在尝试检测文件上次修改时间是否为3分钟.这是我的代码:

if (file_exists($filename)) {
    $filemodtime = filemtime($filename);
}

$three_min_from_now = mktime(0, 3, 0, 0, 0, 0);

if (time() >= $filemodtime + $three_min_from_now) {
     // do stuff
} else {
     // do other stuff
}
Run Code Online (Sandbox Code Playgroud)

但是else条款仍然令人满意,而不是if,即使if应该是真的.我认为这个问题是我的数学问题.有人可以帮忙吗?谢谢.

and*_*wsi 5

$three_min_from_now = mktime(0, 3, 0, 0, 0, 0);

if (time() >= $filemodtime + $three_min_from_now) {
Run Code Online (Sandbox Code Playgroud)

你在这里做的是检查time()是否大于文件修改的unix时间戳,加上从现在开始的三分钟的unix时间戳.这是非常非常不可能的 - 你只需要向$ filemodtime添加180就好了:

if (time() >= $filemodtime + (60 * 3)) {
Run Code Online (Sandbox Code Playgroud)