Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Matt Campbell
9,767 Pointsecho depending on which day of the week it is at a specific time.
I'm doing a website for a friend who own a bar.
What I want to do is put a little neon like sign at the top right of the page to show if the bar is open or closed. Done that, all fine and dandy.
Next step is to add PHP code to change the sign from green open to red closed depending on the day of the week and the time of the day.
I've got the PHP working great if the bar open and closes on the same day. IE, it's opening hours down rollover to the next day.
What I'm struggling with is when the bar opens at 5pm on Friday and shuts at 1am on Saturday.
This is what I'm trying
if ($time > strtotime('5pm') & $day === "Friday" & $time < strtotime('1am') & $day = "Saturday") {
echo '<div class="open pulse">OPEN</div>';
} else {
To me, logic dictates that should work but it isn't. It just says closed even though it should say open as it is now open.
I have 6 statements like that, with the final else at the bottom and a massive line of }}}}}}}
Anyone suggest where I'm screwing up. Learning this today so it's taking some time. What I want to avoid is to have to have two if statements for days where the bar closes in the early morning and opens again that night.
Is that the only option of there another?
One for Randy Hoyt maybe?
Here's the site: http://ultimatewebdevs.co.uk/testspace5/
Thanks all.
3 Answers
Randy Hoyt
Treehouse Guest TeacherRight now, your conditional looks for 4 conditions:
- Is it Friday?
- Is it after 5 PM?
- Is it Saturday?
- Is it before 1:00 AM?
Those won't ever all four be true: it will never be Friday and Saturday! You'll need some ORs in there to make it work.
You've got this working for the other days, but here's the process. You want to check two things:
- Is it after the opening time of Friday at 5?
- Is it before closing time of Saturday at 1?
How do you know if it's after opening time? One of two ways:
- If it's Friday and it's after 5.
- If it's Saturday ... at all. (All of Saturday is after Friday at 5.)
Either one will do it.
($time > strtotime('5pm') && $day === "Friday") || $day === "Saturday"
How do you know if it's before closing time? Also one of two ways:
- If it's Friday ... at all. (All of Friday is before Saturday at 1.)
- If it's Saturday and it's before 1.
$day === "Friday" || ($time > strtotime('1am') && $day === "Saturday")
Lastly, how do you know if it's both after opening time and before closing time? You put those two chunks of code together with an &&.
if ((($time > strtotime('5pm') && $day === "Friday") || $day === "Saturday") && ($day === "Friday" || ($time > strtotime('1am') && $day === "Saturday"))) {
echo '<div class="open pulse">OPEN</div>';
} else {
Something like that should do the trick.
Does that help?
Mike Costa
Courses Plus Student 26,362 PointsHey Matthew,
Does this bar have set hours everyday? example: mon-fri: 5pm-1am sat/sun: 5pm-2am
Matt Campbell
9,767 PointsThat looks brilliant Randy Hoyt. I was trying or but wasn't doing it right. Your explanation looks brilliant. Only had a quick read as I'm off out...had enough computer now, but will digest and implement tomorrow.
Many thanks.