View Full Version : Image change every week

12-03-2009, 03:21 PM
Can anyone link me to code that will change an image everyweek to an image that I define? I have 52 different images, one for every week.

Philip M
12-03-2009, 05:30 PM
Try this:-

<div id = "myimagediv"><img src = "One.gif" id = "myimage"></div>

<script type = "text/javascript">
Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1); // month 0 is January
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);

var today = new Date();
var weekno = today.getWeek();

var images = []
images [1] = "One.gif";
images [2] = "Two.gif"
images [3] = "Three.gif";
images [4] = "Four.gif";
images [5] = "Five.gif";
// and so on for 53 weeks

document.getElementById("myimage").src = images[weekno];



This is based on the week in which January 1st falls as being Week 1, though not all of the days in that week are necessarily in the current year. A week is defined as running from Sunday - Saturday. There are of course 53 weeks in a year (week 53 being fewer than 7 days).

Did we not cover this in http://www.codingforums.com/showthread.php?p=891494?

"I have never taken any exercise except sleeping and resting." - Mark Twain, US humorist, novelist, short story author, & wit (1835 - 1910)

12-03-2009, 06:17 PM
Philip, yes we did, but before I asked for text also. Turns out they made 52 different images for each week. Thanks again for your help, I'll see how it works out.