PHP Check if current time is before specified time

Adam Moss picture Adam Moss · Dec 12, 2011 · Viewed 82.6k times · Source

I need to check in PHP if the current time is before 2pm that day.

I've done this with strtotime on dates before, however this time it's with a time only, so obviously at 0.00 each day the time will reset, and the boolean will reset from false to true.

if (current_time < 2pm) {
   // do this
}

Answer

Tom picture Tom · Dec 12, 2011
if (date('H') < 14) {
   $pre2pm = true;
}

For more information about the date function please see the PHP manual. I have used the following time formatter:

H = 24-hour format of an hour (00 to 23)