
Can I change my Web page based on time of day?Dave, I need your help. I'm updating my Web site for The David Lawrence Show and would like to display 'ON THE AIR' between 7p and 10p PT (3a to 6a GMT) Monday through Saturday, and 'OFF THE AIR' the rest of the time. How do I do this? Later, I can easily replace this displayed text with an image tag once I find an on-air light graphic that doesn't look like it's from 1943. :-) This is a classic situation where you could use a number of different paths to attain the same goal, ranging from a CGI solution to a small server-side include, to a PHP script or, the solution I'll demonstrate here, a JavaScript client-side snippet. Each has its strengths and weaknesses, and for JavaScript the risk is that some percentage of your viewers will probably have JavaScript disabled in their browsers. Assuming you can live with only about 95% of your visitors knowing if you're on the air at that moment, this should work reasonably well, and it's really easy to add without making any changes on the server. There's one other potential hiccup in that since JavaScript is client-side, and since it's working with time, there's no way for the script to know if the visitor has their computer clock set wrong. Ah well. Let's assume that's not a big crisis either, shall we? The first part of this script simply grabs the current time of day then extracts the Universal Time Coordinated (UTC, this used to be called Greenwich Mean Time and then Zulu Time) values for day of week and hour of day. Three simple lines: var thedate = new Date(); var dayofweek = thedate.getUTCDay(); var hourofday = thedate.getUTCHours(); I'll also create a simple function that lets you add conditional HTML content anywhere on the page by simply testing if (onAir()): function onAir()
{
// return TRUE if it's Mon through Sat, 3a-6a UTC
if (dayofweek != 0 &&
((hourofday > 2 && hourofday < 7))) {
return true;
}
return false;
}
To show how this works, here's an entire HTML page that includes this JavaScript and an example of how to use this new function elsewhere in the page: <head>
<script type="text/javascript">
var thedate = new Date();
var dayofweek = thedate.getUTCDay();
var hourofday = thedate.getUTCHours();
function onAir()
{
// return TRUE if it's Mon through Sat, 3a-6a UTC
if (dayofweek != 0 &&
((hourofday > 2 && hourofday < 7))) {
return true;
}
return false;
}
</script>
</head><body>
<h1>The David Lawrence Show!</h1>
<script type="text/javascript">
if (onAir()) {
document.write("<h2>ON THE AIR</h2>");
} else {
document.write("<h2>OFF THE AIR</h2>");
}
</script>
<p>
If you want to learn more about David's show,
please visit
<a href="http://thedavidlawrenceshow.com/">The
David Lawrence Show</a>
</p>
</body>
</html>
I'll leave the rest of it in your capable hands, David, and encourage everyone else to pop over to your sites and learn about both of your great radio programs The David Lawrence Show and Online Tonight.
Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.
Categorized:
CGI Scripts and Web Site Programming
(Article 3893)
Tagged: Previous: How do the costs of publishing a book break down? Next: How do I create custom email filters in MSN Hotmail? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. Dave, how could one accomplish this using PHP? Posted by: Kevin Walter at February 20, 2005 1:30 AMThe PHP version is pretty similar. It makes use of PHP's <a href="http://www.php.net/manual/en/function.date.php">date</a> command. First, here's the onAir() function in PHP: Now, you just use the function above to decide whether to display "On Air" or "Off the Air." <?php Try ZOPE. I need a script based on the Sabbath times- friday sundown to sat sundown, that will forward to a certian page I specify to say temp closed due to sabbath. Here are the times for sundown in my area. 7 Oct 6:27
Perfect! Thanks this HTML example does exactly what I needed! Hello great script, but how do i make it show a particular graphic instead of saying 'on air' eg, I want to add a graphic on a web page that changes as per certain times of the day to a different graphic in the same position and will change back once the time changes.. Paul Posted by: Paul at December 14, 2007 3:44 AMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|