简体   繁体   English

PHP检查时间是否小于特定值?

[英]PHP check time is less than a specific value?

I am trying to compare times and to see if it is less than a set time and if so, do something...How can this be achieved? 我试图比较时间,看看它是否少于设定的时间,如果是,做一些事情......如何实现?

For example I have this so far. 例如,到目前为止,我有这个。

$now = strtotime(date('g:i a'));
$event_time = strtotime("10:00 am");

With these two variables, how can I check if the event_time is 5 minutes BEFORE the current "now" time? 使用这两个变量,如何在当前“现在”时间之前检查event_time是否为5分钟?

Thanks... 谢谢...

You don't need strtotime for the current time. 你现在不需要strtotime Timestamps are values in seconds, just do some math: 时间戳是以秒为单位的值,只需做一些数学运算:

$now = time();
$event_time = strtotime("10:00 am");

if( ($now - $event_time) == (5 * 60)) // 5 minutes * 60 seconds, replace with 300 if you'd like
{
    // 5 minutes before
}

Demo - But you should use the above code for the $now variable. 演示 - 但是您应该将上面的代码用于$now变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM