Jump to content


Photo

Showing "Open" or "Closed" based on hours in a db?


  • Please log in to reply
3 replies to this topic

#1 bpottle

bpottle
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 April 2006 - 04:14 AM

I've got the business hours of several locations that need to be stored in a MySQL database. I'll be using php to display a list of these locations and an image based on whether they are open or closed to indicate such. I figured "hey, this'll be simple," and threw together a table looking something like "sid, sunOpen, sunClose, monOpen, monClose, etc..." planning to simply write a script that got time(); in 24-hour format, checked it against the open and close hours in the database, and display the appropriate image.

...but then I realized that some of the locations are open past midnight on the weekends.

I'm having a real mindblock on this... what would be the most efficient way to do this in a way that accommodates places open into the early hours of the next day?

--Ben

#2 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 13 April 2006 - 10:06 AM

This was easy to code, but i've tried half a dozen times to explain what it does and I can't lol it's hard to explain.

Basically...

1. Is the closing time number less than the opening time number?
YES - We're open after midnight.
NO - We're not open after midnight.

2. Compare the time now against the opening time, and then against the closing time. If it falls between them the place is open, if not, it's closed.

Hope this makes sense for you.


<?php        

if($closing_time<$opening_time)
{        
    if($time_now<$opening_time)
        {
        if($time_now<$closing_time)
            {
            echo "WE'RE OPEN";
            }
        else
            {
            echo "WE'RE CLOSED";
            }
        }
}
else
{
    if($time_now>$opening_time)
        {
        if($time_now<$closing_time)
            {
            echo "WE'RE OPEN";
            }
        else
            {
            echo "WE'RE CLOSED";
            }
        }
}

?>

wisewood: proven fact, I am both wise, and wooden.

#3 bpottle

bpottle
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 April 2006 - 03:31 PM

[!--quoteo(post=364337:date=Apr 13 2006, 06:06 AM:name=wisewood)--][div class=\'quotetop\']QUOTE(wisewood @ Apr 13 2006, 06:06 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
This was easy to code, but i've tried half a dozen times to explain what it does and I can't lol it's hard to explain.

*snip*
[/quote]

Aha! It makes perfect sense! Thanks for your help, this is exactly what I was trying to do :-D

--Ben

#4 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 13 April 2006 - 03:40 PM

happy to help.

I'll probably use it myself at some point.
wisewood: proven fact, I am both wise, and wooden.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users