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:
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.
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
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?
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
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...
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', 16, 30, 17, 30))
{
?>
<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
Bookmarks