douyan8266 2014-02-27 17:34
浏览 89
已采纳

根据时间隐藏div - PHP

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? =/

  • 写回答

2条回答 默认 最新

  • dongmou9260 2014-02-27 17:50
    关注
    if (date("w") == 0 || date("w") == 6 || $currentHour < $openTime || $currentHour >= $closeTime)
    {
        $css = 'display:none;';
    }
    else
    {
        $css = 'display:block;';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用