Support » Plugins » Time based post display

  • Resolved artifacting


    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)
  • 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
    <? } ?>

    Thanks for the suggestion luckdragon. I made a custom template using this code:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    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?

    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?

    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
    <? } ?>

    here is the paste bin for the code to the custom page I created:

    You can see it live here:

    yeah, I see that it’s saying closed, I’ll try it on my end and see

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

    Updated my code accordingly: The live page is still the same.

    Still no luck.

    Any other ideas?

    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.



    Forum Moderator

    try definitively to use:
    $myhour = date('Gi');
    using 24-hr clock like military time;

    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 } ?>

    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.

    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.

    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.