AlwaysLearning AlwaysLearning - 1 year ago 102
PHP Question

PHP display business hours open or closed depending on time

I am trying to get the minutes to work to echo "open" or "closed" depending on time. It works if i do not add the minutes, but not when adding minutes.

$hour = (int) date('H:i');

if ($hour >= 1215 && $hour <=1735) {
// between 8:15am and 5:35pm
echo "Open";
} else {
echo "Closed";

Answer Source

Given a time, for example 18:55, your date('H:i') will output a string, 18:55 when using the format H:i. If you try to cast this to an integer, like you are with (int), it will evaluate just the first number, 18.

Simply remove the colon in your date() function! This will return a string 1855 from the date, which can be cast to an integer of that value. Although, it's not strictly needed to cast it to an integer, as PHP will treat a string of numbers as an integer.

So basically you just need

$hour = date("Hi");

if ($hour >= 1215 && $hour <= 1735) {
    // between 12:15 and 17:35
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download