/    Sign up×
Community /Pin to ProfileBookmark

Loading different images based on time of day

I’m working on a site for our radio station and want it to load a banner where you can see who the DJ is that is on. I saw code on here yesterday (actually it was from March, but I just found it yesterday and can’t today) to do it, it showed two images at different times but I couldn’t get it to work and don’t know enough about java to add more time slots. If someone could help me or point me in the direction of help it’d be greatly appreciated.

Thanks!

Jason

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@JMRKEROct 30.2008 — Try this:
[code=php]
<html>
<head>
<title>Time Of Day Images</title>
<script type="text/javascript">
var DJimages = [ // add path to directory if necessary
"DJimage1.jpg",
"DJimage2.jpg",
"DJimage3.jpg",
"DJimage4.jpg" // NOTE: no comma after last entry
];
function TODimage() {
var now = new Date();
hr = now.getHours(); // alert(hr); // for testing
var DJ = 0; // midnight to 5:59 am
if ((hr >= 6) && (hr < 12)) { DJ = 1; } // 6am to 11:59am
if ((hr >= 12) && (hr < 18)) { DJ = 2; } // Noon to 5:59 pm
if ((hr >= 18) && (hr < 24)) { DJ = 3; } // 6pm to midnight
document.getElementById('DJimage').src = DJimages[DJ];
document.getElementById('DJimage').alt = DJimages[DJ];
}
</script>
</head>
<body onload="TODimage()">
<img id="DJimage" src="" alt="DJimage">
</body>
</html>
[/code]

Good Luck!

?
Copy linkTweet thisAlerts:
@pyro15authorOct 30.2008 — That worked great! I just have to figure out the time diference between my time zone and my server, thanks so much!

Is there a way to make them each link to a different page? Would I add the href tag to the image source in the script at the top or somewhere else? What I want to do with it now is have it so they can click ont he on air person and go to their bio.
Copy linkTweet thisAlerts:
@astupidnameOct 30.2008 — [CODE]<html>
<head>
<title></title>
<script type="text/javascript">
window.onload = function(){TODimage();} //can use the window.onload instead of putting onload in the body tag

function TODimage() {
var DJimages = [ // add path to directory if necessary
{ pic:"DJimage1.jpg", link:"DJ_Al_bio.html" },
{ pic:"DJimage2.jpg", link:"DJ_Bob_bio.html" },
{ pic:"DJimage3.jpg", link:"DJ_Carl_bio.html" },
{ pic:"DJimage4.jpg", link:"DJ_Don_bio.html" } // NOTE: no comma after last entry
];
var now = new Date();
hr = now.getUTCHours();//use UTC hours, then adjust as necessary
var DJ = 0;// midnight to 5:59 am (Central Standard Time - U.S.)
if ((hr >= 11) && (hr < 17)) { DJ = 1; } // 6am to 11:59am (Central Standard Time - U.S. 5 hours diff less than UTC hours)
if ((hr >= 17) && (hr < 23)) { DJ = 2; } // Noon to 5:59 pm (Central Standard Time - U.S.)
if ((hr >= 23) && (hr < 5)) { DJ = 3; } // 6pm to midnight (Central Standard Time - U.S.)
document.getElementById('DJimage').src = DJimages[DJ].pic;
document.getElementById('DJimage').alt = DJimages[DJ].pic;
document.getElementById('DJbioLink').href = DJimages[DJ].link;
}
</script>
</head>
<body>
<a href="DJ_Al_bio.html" id="DJbioLink"><img id="DJimage" src="DJimage1.jpg" alt="DJimage1.jpg" style="border:0;" /></a>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@JMRKEROct 30.2008 — Nice modification while I was at supper 'astupidname' . ?
Copy linkTweet thisAlerts:
@astupidnameOct 31.2008 — Nice modification while I was at supper[/QUOTE]
Hey there, JMRKER, you laid an excellent base! ?
Copy linkTweet thisAlerts:
@Dani_315May 09.2010 — Hi,

I am new comer this site but really find helpful and genious persons around here...

I found one of script ... I mean this one for my site to implement there, but still not success to setting time as per required... Any body would like to help me ... Thanks in advance...

I would like to set time and want to show pictures


FROM :- 01:00 AM - 04:00 PM ( CLOSED SIGN )

FROM :- 04:00 PM - 11:59 PM ( OPEN SIGN )


AS UK LONDON TIME ( GREENWICH TOWN )



Thanks in advance again if somebody help

I been trying to setting time but some times looks OK but not...

My server is in USA and time difference between my server time and LONdon Time about 6 Hours ( Right now in UK time is 16:30 and As my server time after testing is 10:30:03 Server Time )


Please note, I been setting for time difference after asking to support team and they asked me to add +6 hrs in my database config file.... So now Time Print on my invoices As time in UK ..... Hope you understand what I want to explain .... )


Thanks and best regards
Copy linkTweet thisAlerts:
@dtrot55Jun 04.2021 — Hi there @JMRKER

I ran into this code and this would help with my little project I'm working on. Right now though for some reason it seems that it is only grabbing my 3rd image no matter what. At the moment it is showing that the time is "13", which should be image 2, but it is displaying image 3. I'm still tinkering, but this has stumped me at the moment. I'm just using random images at the moment too.

<script type="text/javascript">

var promoimage = [ // add path to directory if necessary

"https://lh3.googleusercontent.com/proxy/max--7h6uDtWUKljLr62O8NAEA22scpSToxqck6ebeb2ayFuxTURDYT6vGhmExgz3k6oLzyKOvaUWh2L8H3-nAl_B90HmN3Ni6lIHrqjnxpPoTKHBhTU",

"https://printables.space/files/uploads/download-and-print/large-printable-numbers/2-a4-1200x1697.jpg",

"https://printables.space/files/uploads/download-and-print/large-printable-numbers/3-a4-1200x1697.jpg"

];

function ChangePromoImage() {

var now = new Date();

var hr = now.getHours();

var daypart = 0;

if ((hr >= 5) && (hr < 9)) { daypart = 1; } // 5am to 9:00am

if ((hr >= 9) && (hr < 17)) { daypart = 2; } // Noon to 4:49 pm

if ((hr >= 17) && (hr < 24)) { daypart = 3; } // 5pm to midnight

document.getElementById('promoimage').src = promoimage[daypart];

document.getElementById('promoimage').alt = promoimage[daypart];

document.getElementById("hour").innerHTML = hr;

}

</script>

</head>

<body onload="ChangePromoImage()">

<img id="promoimage" src="" alt="">

<p>The time is: <span id="hour"></span></p>

</body>
Copy linkTweet thisAlerts:
@dtrot55Jun 04.2021 — @dtrot55#1632549 Think I found my problem, should have been 0, 1, 2 instead of 1,2,3. Seems to be working properly now
Copy linkTweet thisAlerts:
@JMRKERJun 04.2021 — Those are awfully BIG images to be displaying. (Just a note)

What are you really trying do accomplish?

Based upon the code posted, I would assume the time you are testing is between 5pm to midnight to get the 3rd image displayed ()when zero-indexed reference that would be an image of "3"). I'm not sure how you are getting '13' as the program only currently displays one number at a time. I don't see any random display code.

Can you provide the code you are trying to use for testing? Something must be different from original code posted years ago.
Copy linkTweet thisAlerts:
@JMRKERJun 04.2021 — @dtrot55#1632550

You seem to have solved your problem while I was responding :)

As you have noted, all arrays in JS are zero-based.
Copy linkTweet thisAlerts:
@PeaNutsOct 04.2021 — Hello new to this forum, could this be also done on a daily basis Sunday - Saturday then loop. Mike
Copy linkTweet thisAlerts:
@NogDogOct 04.2021 — > @PeaNuts#1637855 could this be also done on a daily basis

Looks like you'd just use the getDay() method (instead of getHours()):

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay
Copy linkTweet thisAlerts:
@drc85Oct 20.2021 — i want to do something similar in adobe animate...

but if i test the html and change the windows time nothing happens...

anybody knows what the problelm could be?
Copy linkTweet thisAlerts:
@JMRKEROct 20.2021 — @drc85#1638376 Might be better to show the problem you are having in a new thread.

This tread was started in 2008 and much in the JS world has changed since then.
×

Success!

Help @pyro15 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 4.18,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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