switch statement for state of day theme

Hi All,

I am trying to setup a simple website that changes colors during the day. I have made a switch statement for this. But the statement only pick the second option. No matter the time of day. What is wrong with this code? In this case it will only take “Dawn” state because it is the second.

[php]

$date = date(“G”);
switch (true) {
// NIGHT 8pm - 4am
case ($date <= 4 || $date >= 20) :
$maincolor = ‘#40AFB5’;
$darkcolor = ‘#08666b’;
$backgroundcolor = $bgcolor;
$backgroundimage = ‘bg’;
$stateofday = ‘Night’;
break;
// DUSK 5pm - 8pm
case ($date <= 20 || $date >= 17) :
$maincolor = ‘#0ea2e8’;
$darkcolor = ‘#016493’;
$backgroundcolor = $bgcolor;
$backgroundimage = ‘bg’;
$stateofday = ‘Dusk’;
break;
// DAWN 4am - 7am
case ($date <= 7 || $date >= 4) :
$maincolor = ‘#d80e0c’;
$darkcolor = ‘#810503’;
$backgroundcolor = $bgcolor;
$backgroundimage = ‘bg’;
$stateofday = ‘Dawn’;
break;
// DAY 7am - 5pm
default:
$maincolor = ‘#f4b41b’;
$darkcolor = ‘#996d02’;
$backgroundcolor = $bgcolor;
$backgroundimage = ‘bg’;
$stateofday = ‘Day’;
}

[/php]

You need to rethink your conditions. For example,

if $date = 2 then conditions 1, 2, and 3 all match because $date <= 4 (and 20, and 7)

hmm thanks! I have changed the OR (||) to AND (&&) and it seems to be doing what I want now.

Thanks

Sponsor our Newsletter | Privacy Policy | Terms of Service