0

I have this code which I used on another site to show the open and closed status for a shop. I am using it for another shop which is open past or up to midnight. Because of this, the code thinks the shop is closed as it is registering the time as after the closed time because it's '00'.

Here is the code:

$todays_date = date("D");

if($todays_date == 'Fri' || $todays_date == 'Sat'){
    $open_hours = '2pm - 1am';
    $open = '14'; 
    $close = '01';
    $close_soon = '00';
} else {
    $open_hours = '4pm - 12am';
    $open = '16'; 
    $close = '00';
    $close_soon = '23';
} 
$hour_now = date("H");

if($hour_now == $close_soon){
    $open_close_text = '<span style="color:#F60"><b>Closing Soon</b></span>';
} else {

if($hour_now > $open && $hour_now < $close){
    $open_close_text = '<span style="color:#090"><b>Open Now</b></span>';
} else {
    $open_close_text = '<span style="color:#F00"><b>Closed Now</b></span>';
}
}

Is there any way to correct this issue? I think I would also have a problem when the day flicks over to Mon after midnight on Sun it would show as closed at midnight but that shouldn't occur?

Thanks.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Snappysites
  • 804
  • 1
  • 10
  • 41

1 Answers1

1

There is a good answer to this similar question here: Determine If Business Is Open/Closed Based On Business Hours

And a PHP class here on GitHub which sounds like it will do what you're after: https://github.com/coryetzkorn/php-store-hours


If you want to do it yourself, however, there's a simple answer (which doesn't handle your weekend/weekday problem) and a more complex answer.

The simple answer is that you need to add a logic block that will recognise when the closing time is before the opening time. Something like:

if ($open < $close) {
    if($hour_now > $open && $hour_now < $close){
        $open_close_text = '<span style="color:#090"><b>Open Now</b></span>';
    } else {
        $open_close_text = '<span style="color:#F00"><b>Closed Now</b></span>';
    }
} else {
    if($hour_now < $open && $hour_now > $close){
        $open_close_text = '<span style="color:#F00"><b>Closed Now</b></span>';
    } else {
        $open_close_text = '<span style="color:#090"><b>Open Now</b></span>';
    }
}

But that won't solve your weekend problem. As is mentioned in the comments, you'd be best off using your times to generate actual timestamps using PHP's built-in strtotime function, and working out what to display from there.

But really, this is a solved problem – most things you'll need to program have already been solved by someone else! So unless this is a learning exercise I recommend you check out the existing solutions up top.

Community
  • 1
  • 1
JoLoCo
  • 1,355
  • 2
  • 13
  • 23
  • Thanks for the answer, I'd feel much more comfortable learning how to do this with my own written scripts rather than using others. I will take a look at the links however and test out the above script. – Snappysites Mar 31 '16 at 18:13
  • 1
    No worries, it's a good way to learn! As I say, it won't fix your weekday/weekend problem though. What I do recommend is that you go down the route of defining 'open' and 'close' points, rather than doing it in the traditional human way, as at the moment the 'close' point for Saturday isn't actually on Saturday, it's on Sunday! Start thinking in 24 hour days, and you'll get the idea. – JoLoCo Mar 31 '16 at 18:17