Jump to content

Recommended Posts

A client of mine requested something for their page that lists their current business hours and whether the store is currently open or not.  I thought I had this worked out fine until I was up late working on another aspect of the page and noticed it listing the store as open when it should not be.  The code is as follows:

 

<?php
$currTime = date("g:i a");
$currDate = date("F jS, Y");
$currHour = date("H");
$currDay = date("l");
$currDayNum = date("d");
$currMonthNum = date("m");
$currStoreState = "<span style=\"color:red\"><strong>CLOSED</strong></span>";
$holCurrDay = "$currMonthNum $currDayNum";

// Holidays are: christmas day, new years day, thanksgiving day, labor day, memorial day, 4th of july, etc -- Still need to set up the math for memorial day, thanksgiving and labor day
$holidays = Array(
"Christmas" => "12 25",
"NewYear" => "01 01",
"July4th" => "07 04"
);

foreach ($holidays as $value) {
    if ($holCurrDay == $value) {
        $isHoliday = true;
    }
}

if ($isHoliday == false) {
    if ($currDay != "Sunday") { // Closed Sundays, so exclude that
        if ($currDay == "Saturday") {
            if (($currHour > 08) && ($currHour < 17)) { // Saturday hours are 9am-5pm
                $currStoreState = "<span style=\"color:green\"><strong>OPEN</strong></span>";
            }
        }
        elseif (($currHour > 07) && ($currHour <19)) { // All other days the store is open 8am-7pm
            $currStoreState = "<span style=\"color:green\"><strong>OPEN</strong></span>";
        }
    }
}

echo "Today is $currDay, $currDate at $currTime<br /><br />We are currently: $currStoreState";
?>

 

The check lists the store as being open starting at 1:00am on Saturdays (however it closes at 5:00 like it should...)

 

What I don't understand is that the code directly below it works just fine, and it's set up exactly the same.  If someone can see my logic error somewhere in there, please let me know!

 

Thanks

Billy

Link to comment
https://forums.phpfreaks.com/topic/67550-solved-weird-issue-with-if-statements/
Share on other sites

You realize that that would always be true, don't you?  Just making sure.

 

Anyway, I removed the preceding 0 from "08" and it seems to run fine again.

 

Still not sure why it will work that way during the week just not on Saturdays.

 

Ahh well, who am I to judge?

You need to use quotes around your strings:

<?php

    $currTime = date("g:i a");
    $currDate = date("F jS, Y");
    $currHour = date("H");
    $currDay = date("l");
    $currDayNum = date("d");
    $currMonthNum = date("m");
    $currStoreState = "<span style=\"color:red\"><strong>CLOSED</strong></span>";
    $holCurrDay = "$currMonthNum $currDayNum";

    // Holidays are: christmas day, new years day, thanksgiving day, labor day, memorial day, 4th of july, etc -- Still need to set up the math for memorial day, thanksgiving and labor day
    $holidays = Array(
        "Christmas" => "12 25",
        "NewYear" => "01 01",
        "July4th" => "07 04",
    );

    foreach ($holidays as $value)
    {
        if ($holCurrDay == $value)
        {
            $isHoliday = true;
        }
    }

    if ($isHoliday == false)
    {
        if ($currDay != "Sunday")
        { // Closed Sundays, so exclude that
            if ($currDay == "Saturday")
            {
                if (($currHour > '08') && ($currHour < '17'))
                { // Saturday hours are 9am-5pm
                    $currStoreState = "<span style=\"color:green\"><strong>OPEN</strong></span>";
                }
            }
            elseif (($currHour > '07') && ($currHour < '19'))
            { // All other days the store is open 8am-7pm
                $currStoreState = "<span style=\"color:green\"><strong>OPEN</strong></span>";
            }
        }
    }
    
    echo "Today is $currDay, $currDate at $currTime<br /><br />We are currently: $currStoreState";
    
?>

 

Sorry for the reformat on coding style, just a habit.

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.