user381800
user381800

Reputation:

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?

Thanks...

Upvotes: 0

Views: 2545

Answers (1)

nickb
nickb

Reputation: 59699

You don't need strtotime for the current time. 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.

Upvotes: 2

Related Questions