I'm trying to display a different header image, depending on the time of day at the moment a visitor comes to my site

e.g.

from before 10pm -> image1.jpg
after 10pm -> image2.jpg

I was wondering if it's possible to check on time, based on the time format, drupal is using? Or should I uses the webserver time to accomplish this?

any php functions I could use are welcome, also of course code examples will help a lot.

cheers !

Comments

jorre’s picture

wake up geezers ;-)

Pakfriet.be
Uzegt.be

shane birley’s picture

I stumbled across this post and thought I would subscribe. It is an interesting idea and I can think of a few places I could use such functionality. I will keep this one in mind.

---
Shane Birley
Left Right Minds
http://www.leftrightminds.com

---
Shane Birley
Left Right Minds
https://www.leftrightminds.com

shane birley’s picture

I did a quick search and found some code that may be useful. I haven't tested any of the code, I am just passing this along. The first is just a simple "day" vs. "night" implementation.

<img src="images/picture<?php $hour = date('H'); // hour of the day, 24 hour clock 

if ($hour > 20 or $hour < 6) {
  
        $timepic = 'night';

} elseif ($hour > 12) {

	$timepic = 'day';

} else {

	$timepic = 'morning';

}

echo $timepic;

?>.jpg">

This version is a more complex idea from a radio station that displays images of their hosts online during their air time:

<?php
$h = date('G'); //set variable $h to the hour of the day
$d = date('w'); //set variable $d to the day of the week.
$year = date('Y'); //set variable $year to the current year
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
// Adjust 2 hour offset for MST below.
$h = $h-2;

// MONDAY SCHEDULE
if ($d == 1 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
else if ($d == 1 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
else if ($d == 1 && $h >= 8 && $h < 12) $img = 'img/hosts/shonw.jpg';
else if ($d == 1 && $h >= 12 && $h < 13) $img = 'img/hosts/pottsie.jpg';
else if ($d == 1 && $h >= 13 && $h < 15) $img = 'img/hosts/mikef.jpg';
else if ($d == 1 && $h >= 15 && $h < 19) $img = 'img/hosts/lizzy.jpg';
else if ($d == 1 && $h >= 19) $img = 'img/hosts/danic.jpg';
else if ($d == 2 && $h < 0) $img = 'img/hosts/danic.jpg';

// TUESDAY SCHEDULE
if ($d == 2 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
else if ($d == 2 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
else if ($d == 2 && $h >= 8 && $h < 12) $img = 'img/hosts/shonw.jpg';
else if ($d == 2 && $h >= 12 && $h < 13) $img = 'img/hosts/pottsie.jpg';
else if ($d == 2 && $h >= 13 && $h < 15) $img = 'img/hosts/mikef.jpg';
else if ($d == 2 && $h >= 15 && $h < 17) $img = 'img/hosts/lizzy.jpg';
else if ($d == 2 && $h >= 17 && $h < 20) $img = 'img/hosts/westmar.jpg';
else if ($d == 2 && $h >= 20) $img = 'img/hosts/danic.jpg';
else if ($d == 3 && $h < 0) $img = 'img/hosts/danic.jpg';

// WEDNESDAY SCHEDULE
if ($d == 3 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
else if ($d == 3 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
else if ($d == 3 && $h >= 8 && $h < 12) $img = 'img/hosts/shonw.jpg';
else if ($d == 3 && $h >= 12 && $h < 13) $img = 'img/hosts/pottsie.jpg';
else if ($d == 3 && $h >= 13 && $h < 15) $img = 'img/hosts/mikef.jpg';
else if ($d == 3 && $h >= 15 && $h < 19) $img = 'img/hosts/lizzy.jpg';
else if ($d == 3 && $h >= 19) $img = 'img/hosts/danic.jpg';
else if ($d == 4 && $h < 0) $img = 'img/hosts/danic.jpg';

// THURSDAY SCHEDULE
if ($d == 4 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
else if ($d == 4 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
else if ($d == 4 && $h >= 8 && $h < 12) $img = 'img/hosts/shonw.jpg';
else if ($d == 4 && $h >= 12 && $h < 13) $img = 'img/hosts/pottsie.jpg';
else if ($d == 4 && $h >= 13 && $h < 15) $img = 'img/hosts/mikef.jpg';
else if ($d == 4 && $h >= 15 && $h < 19) $img = 'img/hosts/lizzy.jpg';
else if ($d == 4 && $h >= 19) $img = 'img/hosts/danic.jpg';
else if ($d == 5 && $h < 0) $img = 'img/hosts/danic.jpg';

// FRIDAY SCHEDULE
if ($d == 5 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
else if ($d == 5 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
else if ($d == 5 && $h >= 8 && $h < 10) $img = 'img/hosts/shonw.jpg';
else if ($d == 5 && $h >= 10 && $h < 12) $img = 'img/hosts/patm.jpg';
else if ($d == 5 && $h >= 12 && $h < 13) $img = 'img/hosts/pottsie.jpg';
else if ($d == 5 && $h >= 13 && $h < 15) $img = 'img/hosts/edp.jpg';
else if ($d == 5 && $h >= 15 && $h < 18) $img = 'img/hosts/lizzy.jpg';
else if ($d == 5 && $h >= 18 && $h < 20) $img = 'img/hosts/jeremyb.jpg';
else if ($d == 5 && $h >= 20 && $h < 22) $img = 'img/hosts/exfyl.jpg';
else if ($d == 5 && $h >= 22) $img = 'img/hosts/stickyb.jpg';
else if ($d == 6 && $h < 0) $img = 'img/hosts/stickyb.jpg';

// SATURDAY SCHEDULE
else if ($d == 6 && $h >= 0 && $h < 4) $img = 'img/hosts/techtronic.jpg';
else if ($d == 6 && $h >= 4 && $h < 5) $img = 'img/hosts/pmw.jpg';
else if ($d == 6 && $h >= 5 && $h < 8) $img = 'img/hosts/geoffh.jpg';
else if ($d == 6 && $h >= 8 && $h < 9) $img = 'img/hosts/tomf.jpg';
else if ($d == 6 && $h >= 9 && $h < 10) $img = 'img/hosts/jimmyj.jpg';
else if ($d == 6 && $h >= 10 && $h < 11) $img = 'img/hosts/jasonr.jpg';
else if ($d == 6 && $h >= 11 && $h < 12) $img = 'img/hosts/hollyk.jpg';
else if ($d == 6 && $h >= 12 && $h < 13) $img = 'img/hosts/tomt.jpg';
else if ($d == 6 && $h >= 13 && $h < 14) $img = 'img/hosts/seanf.jpg';
else if ($d == 6 && $h >= 14 && $h < 15) $img = 'img/hosts/nutmeg.jpg';
else if ($d == 6 && $h >= 15 && $h < 17) $img = 'img/hosts/aaron_jenny.jpg';
else if ($d == 6 && $h >= 17 && $h < 19) $img = 'img/hosts/rayg_adrians.jpg';
else if ($d == 6 && $h >= 19 && $h < 22) $img = 'img/hosts/mattb.jpg';
else if ($d == 6 && $h >= 22) $img = 'img/hosts/hairballj.jpg';
else if ($d == 0 && $h < 0) $img = 'img/hosts/hairballj.jpg';

// SATURDAY SCHEDULE
else if ($d == 0 && $h >= 0 && $h < 2) $img = 'img/hosts/darrelm.jpg';
else if ($d == 0 && $h >= 2 && $h < 4) $img = 'img/hosts/techtronic.jpg';
else if ($d == 0 && $h >= 4 && $h < 5) $img = 'img/hosts/bigjon.jpg';
else if ($d == 0 && $h >= 5 && $h < 6) $img = 'img/hosts/joebear.jpg';
else if ($d == 0 && $h >= 6 && $h < 8) $img = 'img/hosts/russh.jpg';
else if ($d == 0 && $h >= 8 && $h < 9) $img = 'img/hosts/ronk.jpg';
else if ($d == 0 && $h >= 9 && $h < 10) $img = 'img/hosts/rockpoint.jpg';
else if ($d == 0 && $h >= 10 && $h < 11) $img = 'img/hosts/churchatqc.jpg';
else if ($d == 0 && $h >= 11 && $h < 12) $img = 'img/hosts/desertcf.jpg';
else if ($d == 0 && $h >= 12 && $h < 16) $img = 'img/hosts/kristenm.jpg';
else if ($d == 0 && $h >= 16 && $h < 17) $img = 'img/hosts/cdogg.jpg';
else if ($d == 0 && $h >= 17 && $h < 18) $img = 'img/hosts/snarf_daff.jpg';
else if ($d == 0 && $h >= 18 && $h < 19) $img = 'img/hosts/sonicsociety.jpg';
else if ($d == 0 && $h >= 19 && $h < 21) $img = 'img/hosts/jscott.jpg';
else if ($d == 0 && $h >= 21) $img = 'img/hosts/ghostlytalk.jpg';
else if ($d == 1 && $h < 0) $img = 'img/hosts/ghostlytalk.jpg';
?>

---
Shane Birley
Left Right Minds
http://www.leftrightminds.com

---
Shane Birley
Left Right Minds
https://www.leftrightminds.com

shane birley’s picture

And, in case anyone cares, here is another site with time based background images:

http://www.aaronrobbins.com/092007/php-function-time-based-background/

---
Shane Birley
Left Right Minds
http://www.leftrightminds.com

---
Shane Birley
Left Right Minds
https://www.leftrightminds.com