I am trying to make a simple function to output 2 different lines of text depending on the time of the day. I want it to say after 4pm - Next day delivery will be processed the following day.
I have wrote this so far:
<?php
$currentTime = time() + 3600;
echo date('H:i',$currentTime);
?>
However as the date function returns a string, I am unsure of how to use an IF statement to check whether the time is greater than 16:00.
The trick to manipulating and comparing dates and times in PHP is to store date/time values in an integer variable and to use the mktime(), date() and strtotime() functions. The integer repesentation of a date/time is the number of seconds since midnight, 1970-Jan-1, which is referred to as the 'epoch'.
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.
Should do it
if (((int) date('H', $currentTime)) >= 16) {
// .. do something
}
Because PHP is weak-typed you can omit the (int)
-casting.
As a sidenote: If you name a variable $currentTime
, you shouldn't add 1 hour to it, because then its not the current time anymore, but a time one hour in the future ;) At all
if (date('H') >= 16) { /* .. */ }
if ($currentTime > strtotime('16:00:00')) {
// whatever you have to do here
}
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