Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide div based on time - PHP

Tags:

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

like image 730
Xander Avatar asked Dec 27 '25 20:12

Xander


1 Answers

$closeTime should be 21 unless you're only open for an hour

like image 194
MrZebra Avatar answered Dec 30 '25 10:12

MrZebra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!