Title: Time based post display
Last modified: August 20, 2016

---

# Time based post display

 *  Resolved [artifacting](https://wordpress.org/support/users/artifacting/)
 * (@artifacting)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/)
 * I would like to create a blog post or page that displays certain information 
   during a certain time of day. For instance it will display contact information
   from 7am to 2:30pm (during school hours) and store hours with an ad during the
   rest of the day (when school is out).
 * Unfortunately I don’t really even know where to start. I think a small javascript
   could probably do something like this. I’m also open to plugins and hacks.
 * Any ideas?

Viewing 13 replies - 1 through 13 (of 13 total)

 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765829)
 * you might have to create a template specific to that post, and in the template
   put your code, i.e.
 *     ```
       <?
       $myhour = date("H");
       $myminute = date("i");
   
       if ($myhour >= 7 && ($myhour <=2 && $myminute <= 30)) {
       ?>
       html for contact information goes here
       <? } else { ?>
       html for store hours goes here
       <? } ?>
       ```
   
 *  Thread Starter [artifacting](https://wordpress.org/support/users/artifacting/)
 * (@artifacting)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765832)
 * Thanks for the suggestion luckdragon. I made a custom template using this code:
 * _[Code moderated as per the [Forum Rules](http://codex.wordpress.org/Forum_Welcome#Posting_Code).
   The maximum number of lines of code that you can post in these forums is **ten
   lines**. Please use the [pastebin](http://wordpress.pastebin.com/)]_
 * But the only thing is ever says is: “we are closed for business” despite how 
   I tweek the hours in the code I can’t get it to say “we are open for business”.
   Any ideas?
 *  Thread Starter [artifacting](https://wordpress.org/support/users/artifacting/)
 * (@artifacting)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765833)
 * Basically I can’t get it to say anything but the store hours. despite the actual
   time and despite changing the hours around. Does anybody have any ideas?
 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765834)
 * please use pastebin for your code, so that we can see it and hopefully find what
   you are doing wrong
 * if you use the code I gave you, it would be:
 *     ```
       <?
       $myhour = date("H");
       $myminute = date("i");
   
       if ($myhour >= 7 && ($myhour <=2 && $myminute <= 30)) {
       ?>
       we are closed
       <? } else { ?>
       we are open
       <? } ?>
       ```
   
 *  Thread Starter [artifacting](https://wordpress.org/support/users/artifacting/)
 * (@artifacting)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765853)
 * here is the paste bin for the code to the custom page I created:
    [http://pastebin.com/tyNBw7P8](http://pastebin.com/tyNBw7P8)
 * You can see it live here:
    [http://www.seanhubbard.com/blog/stuff/time-based-test-page/](http://www.seanhubbard.com/blog/stuff/time-based-test-page/)
 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765854)
 * yeah, I see that it’s saying closed, I’ll try it on my end and see
 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765855)
 *     ```
       <?
          date_default_timezone_set(get_option('timezone_string'));
                       $myhour = date("H");
                       $myminute = date("i");
       ```
   
 * then make sure to go to your admin area
    Settings -> General and set your timezone
   appropriately (I noticed that the “manual” ones like UTC-5 don’t seem to work,
   but the names like America/New_York do.
 *  Thread Starter [artifacting](https://wordpress.org/support/users/artifacting/)
 * (@artifacting)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765856)
 * Updated my code accordingly: [http://pastebin.com/rX0d8wrY](http://pastebin.com/rX0d8wrY).
   The live page is still the same.
 * Still no luck.
 * Any other ideas?
 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765857)
 * did you make sure to set your timezone?
 * try adding in:
 * echo “\””.get_option(“timezone_string”).”\””;
    to see what it actually is set
   to.
 * also, echo $myhour to see if it’s showing the correct hour.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765858)
 * try definitively to use:
    `$myhour = date('Gi');` using 24-hr clock like [military time](http://en.wikipedia.org/wiki/24-hour_clock);
 * btw: this will give you a weird half-hour rhythm of ‘open/closed’ messages:
    `
   if ($myhour >= 02 && ($myhour <=19 && $myminute <= 30))` because the second bracketed
   part will be false everytime when minutes are larger than 30.
 * I would try (untested – and I don’t use php shorttags):
 *     ```
       <?php   date_default_timezone_set(get_option('timezone_string'));
                $myhour = date("Gi");
                if ($myhour >= 700 && $myhour <= 1430) { ?>
                       We are open for business
                <?php } else { ?>
                       We are closed for business
                <?php } ?>
       ```
   
 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765859)
 * actually, I tried that code on my site last night, and it worked perfectly, it’s
   saying if the hour is greater than 7 and both the hour is less than 7 and minute
   is less than 30, it works fine since it’s grouped together, but yes, military
   will work, but I believe his problem is that WP is giving him UTC so when he 
   tests, if he’s UTC-5, it will say closed even if it’s 2:45 in the afternoon.
 *  [luckdragon](https://wordpress.org/support/users/luckdragon/)
 * (@luckdragon)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765860)
 * also, I noticed that if you change your timezone in the settings area and select
   like UTC-5 as a timezone, it does not enter anything into the options db, so 
   I’m not sure how it knows to keep that value.
 *  Thread Starter [artifacting](https://wordpress.org/support/users/artifacting/)
 * (@artifacting)
 * [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765861)
 * Interesting. Not sure what changed, but adding the “echo time” zone and “echo
   time” seemed to get the code working. I’m testing the minute feature still but
   all seems to be going well.
 * We have working code here.
 * Thank you very very much luckdragon and alchymyth!!! The wordpress community 
   rules!

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘Time based post display’ is closed to new replies.

## Tags

 * [page](https://wordpress.org/support/topic-tag/page/)
 * [post](https://wordpress.org/support/topic-tag/post/)

 * 13 replies
 * 3 participants
 * Last reply from: [artifacting](https://wordpress.org/support/users/artifacting/)
 * Last activity: [14 years ago](https://wordpress.org/support/topic/time-based-post-display/#post-2765861)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
