admin管理员组

文章数量:1123884

I'm trying to use the below code to show a button on Sunday mornings, but I dont want to use "date_default_timezone_set()" because it rewrites the Wordpress date and then the events show the wrong time.

Is there a way to make this code work with the existing Wordpress date settings? When I remove "date_default_timezone_set();" the time goes back to UTC (4 hours ahead) even though Wordpress is set to New York (-4 UTC). Wondering if I need to place this code somewhere other than the header.php...

(I'm a PHP novice.)

<?php 
date_default_timezone_set('America/New_York');
$currentDay = date("w");
$currentHour = date("H");
$openTime = 10; // 10 = 10am
$closeTime = 12; // 12 = 12pm
$serviceDay = 0; // 0 = Sunday

if ($currentHour >= $openTime && $currentHour < $closeTime && $currentDay == $serviceDay){
      $css = 'timeOn';
} else {
      $css = 'timeOff';
} ?>

HTML & CSS

<a class="<?php echo $css; ?>" href="#">
            <div class="liveService"><?php echo 'WATCH LIVE'; ?>  </div>
            </a>

.timeOn {display:block;} 
.timeOff {display:none; }

Code from:

本文标签: phpShow Element at Certain Time in Wordpress