Jump to content

Can anybody review my code and see what im doing wrong


jasonsuthers

Recommended Posts

Can any body help me with the script below, i call it to a web page via Includes newtime.php and it should show a open or closed gif between the hours ive set for each day on that web page
11am - 3am open
3am - 11am Closed

 

 

<?php

$h = date('G'); //set variable $h to the hour of the day
$d = date('w'); //set variable $d to the day of the week.
$year = date('Y'); //set variable $year to the current year
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
 
 
 
// MONDAY SCHEDULE
if ($d == 1 && $h >= 11 && $h < 3) $img = 'open.gif';
else if ($d == 1 && $h >= 3 && $h < 11) $img = 'closed.gif';
else if ($d == 2 && $h < 11) $img = 'open.gif';
 
// TUESDAY SCHEDULE
if ($d == 2 && $h >= 11 && $h < 3) $img = 'open.gif';
else if ($d == 2 && $h >= 3 && $h < 11) $img = 'closed.gif';
else if ($d == 3 && $h < 11) $img = 'open.gif';
 
// WEDNESDAY SCHEDULE
if ($d == 3 && $h >= 11 && $h < 3) $img = 'open.gif';
else if ($d == 3 && $h >= 3 && $h < 11) $img = 'closed.gif';
else if ($d == 4 && $h < 11) $img = 'open.gif';
 
// THURSDAY SCHEDULE
if ($d == 4 && $h >= 11 && $h < 3) $img = 'open.gif';
else if ($d == 4 && $h >= 3 && $h < 11) $img = 'closed.gif';
else if ($d == 5 && $h < 11) $img = 'open.gif';
 
// FRIDAY SCHEDULE
if ($d == 5 && $h >= 11 && $h < 3) $img = 'open.gif';
else if ($d == 5 && $h >= 3 && $h < 11) $img = 'closed.gif';
else if ($d == 6 && $h < 11) $img = 'open.gif';
 
// SATURDAY SCHEDULE
if ($d == 6 && $h >= 11 && $h < 3) $img = '/open.gif';
else if ($d == 6 && $h >= 3 && $h < 11) $img = '/closed.gif';
else if ($d == 7 && $h < 11) $img = '/open.gif';
 
// SUNDAY SCHEDULE
if ($d == 7 && $h >= 11 && $h < 3) $img = '/open.gif';
else if ($d == 7 && $h >= 3 && $h < 11) $img = '/closed.gif';
else if ($d == 1 && $h < 11) $img = '/open.gif';
 
?>
<html>
...
<body>
...
<img src="<?php echo $img; ?>">
...
</html>

Please use code tags when posting source code; it will be much easier to read

 

// MONDAY SCHEDULE
if ($d == 1 && $h >= 11 && $h < 3) $img = 'open.gif';
else if ($d == 1 && $h >= 3 && $h < 11) $img = 'closed.gif';
else if ($d == 2 && $h < 11) $img = 'open.gif';
It appears to me that you are confusing the AM/PM hours. In the first line, $h can NOT be GREATER THAN 11 AND LESS THAN 3 -- perhaps that should be LESS THAN 15? I can't really tell what you are trying to do with the third line, the section comment says MONDAY, but it looks like you are testing TUESDAY ($d == 2).

 

Personally, I would set the default image to closed.gif, and create an array of Open Hours. Then check the array to see if we should switch to the open.gif image. This way, if the hours change, you only have to change the DATA (in the array) instead of having to re-write a bunch of IF statements.

 

Something like:

<?php
/* Array of hours we are open:
The key is the Day of week (date('w'))
The values are arrays - first entry is Open time, second entry is Close time - 24-hour format (date('G'))
*/
$openHrs = array([1] => array(11, 15), // Monday 11 AM - 3 PM
[2] => array(11, 15),
[3] => array(11, 15),
[4] => array(11, 15),
[5] => array(11, 15),
[6] => array(9, 14), // Saturday 9 AM - 2 PM
[7] => array(12, 16), // Sunday Noon - 4 PM
);

$h = date('G'); //set variable $h to the hour of the day
$d = date('w'); //set variable $d to the day of the week.

$image = 'closed.gif';
if (isset($openHrs[$d])) {
if ( ($h >= $openHrs[$d][0]) and ($h < $openHrs[$d][1]) ) $image = 'open.gif';
}

 

I changed up the hours a bit as an example.

 

Obviously if you have any split-days -- 11AM to 3PM then 6PM to 10PM -- you will have to re-work it a bit.

 

Disclaimer: This code is un-tested.

Also, I don't write code like that, it was indented and pretty when I pasted it, this @#$%&! editor keeps screwing with my code.

try

 

    $t1 = new DateTime();
    $t1->setTime(3,0,0);
    $t2 = new DateTime();
    $t2->setTime(11,0,0);
    $now = new DateTime();
    
    if ($now >= $t1 && $now < $t2) {
        $image = 'closed.gif';
    } else {
        $image = 'open.gif';
    }
    echo "<img src='$image' />";

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.