Hide div based on time - PHP

房东的猫 提交于 2019-12-22 17:17:48

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!