我在保存的文本文件中有一个数据 date("Y-m-d h:i:s", strtotime("+2 minutes")),我需要检查它是否是10分钟前.我正在尝试以下代码,但即使超过10分钟,它也不会打印任何内容.
$now = date("Y-m-d h:i:s", strtotime("now"));
if($now > strtotime($old_data))
echo 'expired!';
Run Code Online (Sandbox Code Playgroud)
小智 8
您将格式化日期与时间戳进行比较,这解释了为什么没有任何作用
这里:
$now = strtotime("-10 minutes");
if ($now > strtotime($old_data) {
echo 'expired!';
}
Run Code Online (Sandbox Code Playgroud)
您应该更改以下任一内容:
$now = strtotime(date("Y-m-d h:i:s", strtotime("now")));
或者
if (strtotime($now) > strtotime($old_data))
我去第二个。您正在比较时间戳和日期,这就是您不满足 条件的原因if()。
time()此外,如果您只关心当前时间戳,也可以使用。
$now = time();
或者
$now = date("Y-m-d h:i:s", strtotime("now")); // Remove this line
if(time() > strtotime($old_data)) // Change $now with time()
Run Code Online (Sandbox Code Playgroud)