Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/194373-a-beginner-needs-help/
Share on other sites

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";

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.