WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Time based post display (14 posts)

  1. artifacting
    Member
    Posted 2 years ago #

    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?

  2. luckdragon
    Member
    Posted 2 years ago #

    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
    <? } ?>
  3. artifacting
    Member
    Posted 2 years ago #

    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?

  4. artifacting
    Member
    Posted 2 years ago #

    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?

  5. luckdragon
    Member
    Posted 2 years ago #

    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
    <? } ?>
  6. artifacting
    Member
    Posted 2 years ago #

    here is the paste bin for the code to the custom page I created:
    http://pastebin.com/tyNBw7P8

    You can see it live here:
    http://www.seanhubbard.com/blog/stuff/time-based-test-page/

  7. luckdragon
    Member
    Posted 2 years ago #

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

  8. luckdragon
    Member
    Posted 2 years ago #

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

  9. artifacting
    Member
    Posted 2 years ago #

    Updated my code accordingly: http://pastebin.com/rX0d8wrY. The live page is still the same.

    Still no luck.

    Any other ideas?

  10. luckdragon
    Member
    Posted 2 years ago #

    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.

  11. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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 } ?>
  12. luckdragon
    Member
    Posted 2 years ago #

    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.

  13. luckdragon
    Member
    Posted 2 years ago #

    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.

  14. artifacting
    Member
    Posted 2 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic