问题
I'm using Magento, and am trying to hide a div based on time using PHP. I found this bit of code
<?php date_default_timezone_set('America/New_York');
$currentHour = date("H");
$openTime = 8;
$closeTime = 9;
if ($currentHour >= $openTime && $currentTime < $closeTime){
$css = 'display:block;';
}else{
$css = 'display:none;';
}
echo '<style type="text/css">.timeBasedLink {'.$css.'}</style>'; ?>
And then when I view my websites source (even though right now it is after 9 am, which I've set the time to in PHP just so I could test if it hides, we actually close at 6), it shows up in the header as
<style type="text/css">.timeBasedLink {display:block;}</style>
no matter what time it is it always shows up as display: block. Might be an error in my code, as I'm still learning PHP.
EDIT: Also does anyone know how to change this to hide it all day Saturday and Sunday? =/
回答1:
if (date("w") == 0 || date("w") == 6 || $currentHour < $openTime || $currentHour >= $closeTime)
{
$css = 'display:none;';
}
else
{
$css = 'display:block;';
}
回答2:
$closeTime
should be 21 unless you're only open for an hour
来源:https://stackoverflow.com/questions/22076037/hide-div-based-on-time-php