www.webdeveloper.com
Results 1 to 6 of 6

Thread: show/hide recurring content based on server time

  1. #1
    Join Date
    May 2010
    Posts
    3

    Arrow show/hide recurring content based on server time

    New here, and know nothing about PHP. But I believe PHP is what I need to accomplish this task.

    I need to show/hide some content (in my case a graphical button), based on a recurring weekly event. My client is doing a live streaming video cast every Thursday from 3-4. She wants a button to appear ("click here to tune in LIVE!") for just that duration of time. When her show is over at 4:00, the button disappears until the next Thursday at 3:00 (This is all in US Central Time).

    Of course I could do this manually each week, but that is inefficient and unreliable, so I'm looking for a way to automate this task.

    I have been searching for hours and have come up empty. The one thing that was closest was this thread:

    http://www.webdeveloper.com/forum/sh...d.php?t=103041

    which sounds like what I need to do, but it is using javascript instead of PHP. I would be fine with javascript, except that it uses the web visitor's computer clock, which could be inaccurate and doesn't handle the fact that vistors could be in a different time zone. I need it to be based on the server's clock instead.

    So, can anybody point me in the right direction using PHP to do this? Again, I know very little about PHP, so please be patient with me.

    Thanks in advance.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,251
    PHP Code:
    <?php
    if(date('l') == 'Thursday' and date('G') == 15) {
    ?>
    <button type="button" onclick="blah blah blah">
    <img src="click.png" alt="watch video" />
    </button>
    <?php
    }
    ?>
    If your server is not in the same time zone, you may want to first use date_default_timezone_set() to set the desired time zone.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2010
    Posts
    3
    Thanks NogDog. I will see if this helps and report back either way. However, does this address the issue of having the button disappear when the show is over at 4:00? As mentioned, I don't know much about PHP, but I don't see anything in your code that appears to address that part. How can I set a duration, or expiration date?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,251
    It will only show if the hour is 15 (3pm), so it will show from Thursday 15:00:00 until Thursday 15:59:59.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2010
    Posts
    3
    Oh great, thanks! I'm so grateful for your help. Is there a way to be more specific though? The only reason I ask is because she has another show that starts on the half hour, 11:30 am -12:30 pm that she also wants to do this for. I assume the same code won't work for that one, without modifying it somehow...

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,251
    You might want to make a function that can be used for any case:
    PHP Code:
    <?php
    /**
     * Determine if current time is within specified weekday and time window
     * @return bool
     * @param string  $dayName       'Monday', 'Tuesday',...
     * @param int     $startHour     0-23
     * @param int     $startMinute   0-59
     * @param int     $endHour       0-23
     * @param int     $endMinute     0-59
     */
    function isInTimeWindow($dayName$startHour$startMinute$endHour$endMinute)
    {
       
    $dayName ucfirst($dayName);
       list(
    $dayNow$timeNow) = explode('|'date('l|Hi'));
       return (
          
    ucfirst($dayName) == $dayNow and
          
    $timeNow >= sprintf('%02d%02d'$startHour$startMinute) and
          
    $timeNow <= sprintf('%02d%02d'$endHour$endMinute)
       );
    }
    // Sample usage:
    if(isInTimeWindow('thursday'16301730))
    {
    ?>
    <button>Test</button>
    <?php
    }
    ?>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles