Jump to content

A beginner needs help


anselmiina

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.