/    Sign up×
Community /Pin to ProfileBookmark

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:

[URL=”http://www.webdeveloper.com/forum/showthread.php?t=103041″]http://www.webdeveloper.com/forum/showthread.php?t=103041[/URL]

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.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 13.2010 — [code=php]
<?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
}
?>
[/code]

If your server is not in the same time zone, you may want to first use [url=http://www.php.net/manual/en/function.date-default-timezone-set.php]date_default_timezone_set()[/url] to set the desired time zone.
Copy linkTweet thisAlerts:
@n23authorMay 13.2010 — 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?
Copy linkTweet thisAlerts:
@NogDogMay 13.2010 — It will only show if the hour is 15 (3pm), so it will show from Thursday [B]15[/B]:00:00 until Thursday [B]15[/B]:59:59.
Copy linkTweet thisAlerts:
@n23authorMay 13.2010 — 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...
Copy linkTweet thisAlerts:
@NogDogMay 13.2010 — You might want to make a function that can be used for any case:
[code=php]
<?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', 16, 30, 17, 30))
{
?>
<button>Test</button>
<?php
}
?>
[/code]
×

Success!

Help @n23 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...