scriptygoddess

12 Feb, 2003

Display different text or images based on the time

Posted by: Jennifer In: Scripts

Pretty simple script. This is in response to a script request. Show a different image, or display a different line of text – (maybe even a different color for your site!) depending on the time. Very simple. All based on the date() function in php.

<?
$hour = date('G');
//this will get the hour – 0 – 23
if ($hour < 12) {
// morning between midnight and noon
// show "morning stuff"
?>
<p>Good Morning!</p>
<img src="/images/sunrise.gif">
<?
} else if ($hour == 12 || $hour < 17) {
// between noon and 5
?>
<p>Good Afternoon!</p>
<img src="/images/beach.gif">
<?
} else if ($hour == 17 || $hour <= 23) {
// between 5 and midnight
?>
<p>Good Night!</p>
<img src="/images/moon.gif">
<?
} //end script
?>

4 Responses to "Display different text or images based on the time"

1 | Andrew

February 12th, 2003 at 10:56 pm

Avatar

Hi Jenn. Thanks for the script! Wonderful! :)

How about loading an image depending on the user's screen resolution? 😉

2 | kristine

February 12th, 2003 at 11:22 pm

Avatar

Very cool :) I have a similar one that changes the wording based on the time up here :)

3 | Jennifer

February 13th, 2003 at 8:08 am

Avatar

that's so funny – because I THOUGHT you had done something similar. I had searched for it, but nothing came up.. (I think I have to do some tweaks to the search and see if it's working ok) – but actually, now that I look at it, yours will display different wording based on when the *MT post was made* – mine will display based on the current time (MT independent – and dynamic)…

And actually that's a note I'm sure someone will make – this will grab the *server's* time – which may or may not be the same as YOUR time or your VISITOR'S time. If you want to grab your VISITOR'S time – I think you'll have to do that with Javascript.

4 | Bern

October 16th, 2004 at 2:47 pm

Avatar

Hi,
I hope you don't mind my asking here, but I'm looking for a script that can accomodate a weekly radio schedule. Depending on the day _and_ the time, it should display the title of the radioshow, some more info (like the name of the dj and soforth) and a picture.

So for the time part this script should do the trick, but how can I adapt it so it takes the days into account? It's a weekly schedule, so no need for dates or anything. I hope you can help me out with this. Either way, thanks in advance.

Bern Kreleger
The Netherlands

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements