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.)

$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';
} ?>


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

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

Code from: https://stackoverflow.com/questions/28236466/show-and-hide-div-based-on-day-and-time

  • date_default_timezone_set shouldn't be used, there is no function you should call instead, just remove that line entirely and don't put anything in its place. It's there to setup the timezone so that it matches what you expect but WordPress already does this. What you're trying to do won't be reliable though, as it won't be compatible with caching, and only works if the user is in the same timezone as your site. Javascript + CSS would be much better for this, especially since you can hide the button once when it turns to the afternoon if the tab gets left open
    – Tom J Nowell
    Mar 23 at 14:57
  • @TomJNowell Yes, I'm trying to avoid it, but this was the only code I was able to find that accomplished the client's ask. If you know of a better solution, I'm very open but have not yet found any with Javascript + CSS. Mar 23 at 15:23
  • I think the solution to this has nothing to do with WordPress, none of your PHP code specifies timezones so it assumes the servers timezone, and since you aren't using any of the WordPress date/time functions the timezone set in the admin area is irrelevant. Honestly the javascript version of this will be 10x easier to build and more reliable. Use it to put the day of the week as a CSS class on the body tag and you've done half the job already
    – Tom J Nowell
    Mar 23 at 19:15
  • Id love to do Javascript if you can provide an example, but I haven't found anything. The timezone isn't important except that without it the calculation uses UTC and not UTC-4. Mar 25 at 23:06
  • that's only because you didn't specify the timezone you wanted when calling date, if you want to do this in pure PHP functions without WordPress functions you could ask at stackoverflow, and there's a very high chance someone has already asked this there
    – Tom J Nowell
    Mar 26 at 9:37


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.