anselmiina Posted March 6, 2010 Share Posted March 6, 2010 I'm trying to figure how to create a script that prints opening hours based on what weekday it is. This is the base idea but I don't know how to write it with php: If it is monday-friday print X If it is saturday print Y If it is sunday print Z If it is 2010/3/29 print Y (same opening hours than on saturday even it is monday) If it is 2010/3/30 print Z (same opening hours than on sunday even it is tuesday) If it is 2010/3/31 print G X = 10 am to 7pm Y = 10 am to 5pm Z = 12 to 4pm G = closed Quote Link to comment https://forums.phpfreaks.com/topic/194373-a-beginner-needs-help/ Share on other sites More sharing options...
Zane Posted March 6, 2010 Share Posted March 6, 2010 You'd need an array for each set of open/close hours So for example.. The Monday - Friday array would look like this $monfri['open'] = "10AM"; $monfri['close'] = "7PM"; $operatingHours = array(); Same concept for saturday and sunday.. As for the special days, you'll have to explicitly check for those days on load time.... when the page loads, and set them accordingly using the strftime and date functions something like this $date = strftime('%u'); if ($date >=1 AND $date if ($date == 6) $operatingHours = $sat; if ($date == 7) = $sun; $date = strftime('%x'); //For the strict dates if ($date = "3/29/2010") $operatingHours = $sat; //etcetera etcetera //If the operatingHours is empty then the store is closed if(empty($operatingHours)) echo "The store is closed"; Quote Link to comment https://forums.phpfreaks.com/topic/194373-a-beginner-needs-help/#findComment-1022476 Share on other sites More sharing options...
ialsoagree Posted March 6, 2010 Share Posted March 6, 2010 This should do it... (it's untested though, pretty sure the logic is sound, might be a syntax error or two) if (date('j m y') == '31 3 10') { $hours = 'closed'; } elseif (date('j m y') == '30 3 10') { $hours = '12 to 4pm'; } elseif (date('D') == 'Sat' || date('j m y') == '29 3 10')) { $hours = '10 am to 5pm'; } elseif (date('D') != 'Sat' && date('D') != 'Sun') { $hours = '10 am to 7pm'; } else { $hours = '12 to 4pm' } //Do something with $hours Quote Link to comment https://forums.phpfreaks.com/topic/194373-a-beginner-needs-help/#findComment-1022491 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.