Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Check if current time is before specified time

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 } 
like image 347
Adam Moss Avatar asked Dec 12 '11 13:12

Adam Moss


People also ask

How do you check if a time is between two times in PHP?

As you can see we have used the inbuilt function DateTime::createFromFormat() or strtotime() of PHP to parse the time string according to the required format it returns the result. Getting the time difference between two time slots in PHP, You can use strtotime() for time calculation.

What is strtotime function in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How do you check the date is today or not in PHP?

Answer: Use the PHP date() Function You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.


2 Answers

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)

like image 83
Tom Avatar answered Oct 12 '22 00:10

Tom


Try:

if(date("Hi") < "1400") { } 

See: http://php.net/manual/en/function.date.php

H   24-hour format of an hour with leading zeros    00 through 23 i   Minutes with leading zeros                      00 to 59 
like image 34
Merlyn Morgan-Graham Avatar answered Oct 11 '22 23:10

Merlyn Morgan-Graham