hinchcliffe Posted July 30, 2009 Share Posted July 30, 2009 Hello, I'm kind of a noob at php. I'm trying to write some code that shows what a restaurant special is between certain times of the week and it switches depending on the day of the week as well. Each day of the week will have different specials. There's a breakfast special between 6am-11am, then there's a lunch special between 11am-4pm, then there's a dinner special from 4pm - 11pm. Anyways I've been struggling with some code, could someone take a look at it and help me out? Thanks! <?php $day = (date("D")); $time = (date("H") + 2); // Actuall Specials // Mon if ($day = 'Mon' && $time == (11 > $time && $time > 0)) { $special = 'Monday Breakfast Special'; } if ($day = 'Mon' && $time == (16 > $time && $time > 11)) { $special = 'Monday Lunch Special'; } if ($day = 'Mon' && $time == (24 > $time && $time >= 16)) { $special = 'Monday Dinner Special'; } // Tue if ($day = 'Tue' && $time == (11 > $time && $time > 0)) { $special = 'Tuesday Breakfast Special'; } if ($day = 'Tue' && $time == (16 > $time && $time > 11)) { $special = 'Tuesday Lunch Special'; } if ($day = 'Tue' && $time == (24 > $time && $time >= 16)) { $special = 'Tuesday Dinner Special'; } // Wed if ($day = 'Wed' && $time == (11 > $time && $time > 0)) { $special = 'Wednesday Breakfast Special'; } if ($day = 'Wed' && $time == (16 > $time && $time > 11)) { $special = 'Wednesday Lunch Special'; } if ($day = 'Wed' && $time == (24 > $time && $time >= 16)) { $special = 'Wednesday Dinner Special'; } // Thu if ($day = 'Thu' && $time == (11 > $time && $time > 0)) { $special = 'Thursday Breakfast Special'; } if ($day = 'Thu' && $time == (16 > $time && $time > 11)) { $special = 'Thursday Lunch Special'; } if ($day = 'Thu' && $time == (24 > $time && $time >= 16)) { $special = 'Thursday Dinner Special'; } // Fri if ($day = 'Fri' && $time == (11 > $time && $time > 0)) { $special = 'Friday Breakfast Special'; } if ($day = 'Fri' && $time == (16 > $time && $time > 11)) { $special = 'Friday Lunch Special'; } if ($day = 'Fri' && $time == (24 > $time && $time >= 16)) { $special = 'Friday Dinner Special'; } // Sat if ($day = 'Sat' && $time == (11 > $time && $time > 0)) { $special = 'Saturday Breakfast Special'; } if ($day = 'Sat' && $time == (16 > $time && $time > 11)) { $special = 'Saturday Lunch Special'; } if ($day = 'Sat' && $time == (24 > $time && $time >= 16)) { $special = 'Saturday Dinner Special'; } // Sun if ($day = 'Sun' && $time == (11 > $time && $time > 0)) { $special = 'Sunday Breakfast Special'; } if ($day = 'Sun' && $time == (16 > $time && $time > 11)) { $special = 'Sunday Lunch Special'; } if ($day = 'Sun' && $time == (24 > $time && $time >= 16)) { $special = 'Sunday Dinner Special'; } echo $day; echo ('<br /><br />'); echo $time; echo ('<br /><br />'); echo $special; ?> Any help teaching me would be very appreciated. Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted July 30, 2009 Share Posted July 30, 2009 So what happens now? What is the actual problem? Quote Link to comment Share on other sites More sharing options...
hinchcliffe Posted July 30, 2009 Author Share Posted July 30, 2009 Ok, So what's happening is... It's showing the very last day & not filtering the day if statement. So right now it's supposed to be showing Thursday, Breakfast Special It's showing Sunday, Breakfast Special. Oh it's also not echo'ing the day*. Know what's wrong? Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 30, 2009 Share Posted July 30, 2009 It doesn't echo the special because his variable $special is empty. It's empty because his if conditions are all written incorrectly and none of them are passing or the wrong one(s) are passing. If you want to check if $time is between, for example, 9 and 15 inclusive, you'd use: if( $time >= 9 && $time <= 15 ) { Quote Link to comment Share on other sites More sharing options...
hinchcliffe Posted July 30, 2009 Author Share Posted July 30, 2009 Cool Thanks, Roopurt. Here's the code I have now that works. <?php $day = (date("D")); $time = (date("H") + 2); // Actuall Specials // Test Code if( $time >= 9 && $time <= 15 ) { // Mon if ($day == 'Mon'){ if ($time >= 0 && $time <= 11 ) { $special = 'Monday Breakfast Special'; }} if ($day == 'Mon'){ if ($time >= 11 && $time <= 16 ) { $special = 'Monday Lunch Special'; }} if ($day == 'Mon'){ if ($time >= 16 && $time <= 24 ) { $special = 'Monday Dinner Special'; }} // Tue if ($day == 'Tue'){ if ($time >= 0 && $time <= 11 ) { $special = 'Tuesday Breakfast Special'; }} if ($day == 'Tue'){ if ($time >= 11 && $time <= 16 ) { $special = 'Tuesday Lunch Special'; }} if ($day == 'Tue'){ if ($time >= 16 && $time <= 24 ) { $special = 'Tuesday Dinner Special'; }} // Wed if ($day == 'Wed'){ if ($time >= 0 && $time <= 11 ) { $special = 'Wednesday Breakfast Special'; }} if ($day == 'Wed'){ if ($time >= 11 && $time <= 16 ) { $special = 'Wednesday Lunch Special'; }} if ($day == 'Wed'){ if ($time >= 16 && $time <= 24 ) { $special = 'Wednesday Dinner Special'; }} // Thu if ($day == 'Thu'){ if ($time >= 0 && $time <= 11 ) { $special = 'Thursday Breakfast Special'; }} if ($day == 'Thu'){ if ($time >= 11 && $time <= 16 ) { $special = 'Thursday Lunch Special'; }} if ($day == 'Thu'){ if ($time >= 16 && $time <= 24 ) { $special = 'Thursday Dinner Special'; }} // Fri if ($day == 'Fri'){ if ($time >= 0 && $time <= 11 ) { $special = 'Friday Breakfast Special'; }} if ($day == 'Fri'){ if ($time >= 11 && $time <= 16 ) { $special = 'Friday Lunch Special'; }} if ($day == 'Fri'){ if ($time >= 16 && $time <= 24 ) { $special = 'Friday Dinner Special'; }} // Sat if ($day == 'Sat'){ if ($time >= 0 && $time <= 11 ) { $special = 'Saturday Breakfast Special'; }} if ($day == 'Sat'){ if ($time >= 11 && $time <= 16 ) { $special = 'Saturday Lunch Special'; }} if ($day == 'Sat'){ if ($time >= 16 && $time <= 24 ) { $special = 'Saturday Dinner Special'; }} // Sun if ($day == 'Sun'){ if ($time >= 0 && $time <= 11 ) { $special = 'Sunday Breakfast Special'; }} if ($day == 'Sun'){ if ($time >= 11 && $time <= 16 ) { $special = 'Sunday Lunch Special'; }} if ($day == 'Sun'){ if ($time >= 16 && $time <= 24 ) { $special = 'Sunday Dinner Special'; }} echo $day; echo ('<br /><br />'); echo $time; echo ('<br /><br />'); echo $special; ?> I also had to change up the $day = to $day == Thanks for the help. Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 30, 2009 Share Posted July 30, 2009 Oh. I missed the assignment operator misuse in your conditional test; shame on me! Anyways you can always rewrite: if( $c ) { if( $a && $b ) { } } as if( $c && $a && $b ) { } They're logically equivalent. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.