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 }
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.
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.
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.
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)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With