MikroSight.com

MikroSight.com

| Home | NPA/NPX |

Google
Web
MikroSight

NEW & Improved
Weekly Image Script

This is an improved version of my script that calls a different image for each week of the year.  The original script uses a simple calculation and changes the image file name on the same day of the week that the first day of the year falls on.  For example, in 2005, January the 1st was on Saturday, so the original script would calculate a different file name from 1-52 on every Saturday.

This new script is basically the same except you can specify a "Day of the Week" and the Hour that you want the image to change - for example, every Friday at 3:00pm.

This script still generates 52 file names - in most cases either image1 or image 52 will be used 8 days (to handle the odd day of the year) - and even 9 days in a leap year.

I basically had to add some logic to handle if today is the Day of the week to change on, then ask is it before or after the specified hour to change. If someone wanted to, this concept could be carried all the way down to the minute, second, or millisecond level I guess - but that seems a bit extreme for what this script was/is intended for.

NOTE: Be aware that because this is JavaScript, the time used here is the current system time of the client's computer wherever they are in the world.

Here are some test tables that run every day for each year listed and show the calculated week number for a time before and after the specified "Hour To Change".

Current Time =
Midnight Time =

Current DOW =

Change on DOW =

doya % 7 =

Calculated Day of Year before any adjustment =

Calculated Week of Year before any adjustment =

So, if the Current DOW and Change on DOW above are equal, I use the following values to decide if it's time to change the image file name.

Current Hour =

Change when Equal or Greater than this Hour =

So, the calculated image file name to use for this week is

and the results of our image on this page

On this page, the DOW and Hour to Change on are set for Wednesday at 4 pm - so you can test this by changing your system time to Wednesday at 3 pm and refresh the page, then change it to Wednesday at 4 pm and refresh - you should get a different image shown above.

And here's the script without comments so it's easy to cut & paste. See below for more information on what you can/need to change for use on your site. As always, contact me if you have problems or questions. 

When you use this (and I know you will) please consider making a small donation - even $1 would help -

I would appreciate you leaving my credits in there when you use this. 

<SCRIPT LANGUAGE="JavaScript" type="text/JavaScript">

<!-- Written by J.M. Royalty, 2005 -->
<!-- Stolen from http://mikrosight.com/scripts/weeklyimage/newweeklyimage.shtml -->
<!-- Begin

var DOWtoChange = 3
var HourToChange = 16

var imagepath = 'img/'
var imagebasename = 'image'
var imageextension = '.gif'

// -------------- DO NOT CHANGE BELOW ---------------------
var now = new Date()
var Fday = new Date(now.getYear(), 0, 1, 0, 0, 0)
Fday = Fday.getDay()
var midnight = new Date()
midnight.setHours(0, 0, 0, 0)
var days = Math.ceil(midnight.valueOf() / 86400000)
var leapoff = Math.ceil((midnight.getFullYear() - 1972) / 4)
var doy = (days - leapoff) % 365
if ( doy == 0 ) { doy = 365 }
if ( ( doy == 1 ) && ( now.getDate() > 1 ) ) { doy = 366 }
doya = doy + ( (Math.abs(DOWtoChange - Fday - 6) + 7) % 7 )

wk = Math.floor(doya / 7)

if ( ( doya % 7 ) == 0 )
{ if ( now.getHours() < HourToChange )
{ wk -= 1 }
}
if ( wk == 0 ) { wk = 52 }
if ( wk == 53 ) { wk = 1 }

var imagefilename = imagepath + imagebasename + eval(wk) + imageextension
// -->
</script>

You can put the above script anywhere on the page, either in the <HEAD> section or the <BODY> section.   Just be sure it is above where you want to call the image - I almost always put my scripts like this in the <HEAD> section.

Here's some info on the variables you can/need to change


DOWtoChange - the day of the week you want the image file name to change. Valid values are 0-6 where 0=Sun, 1=Mon, 2=Tues, 3=Wed, 4=Thurs, 5=Fri, 6=Sat

HourToChange - the hour of the day when you want the image file name to change. Valid values are 0-23 where 0=midnight, 23=11pm

imagepath = the directory (folder), relative to your root web where the images are stored this value is added to the beginning of the image file name - so don't forget the trailing slash

imagebasename = this script generates one of 52 file names - all are the same except for a numeric value - if your imagebasename is "mypic", then the script will create a file name of mypic?? where ?? is a value of 1-52 - NOTE: there is no leading zero for values of 1-9 - mypic1, mypic2, ... mypic52

imageextension = this is the type of image and can be whatever you need, as in .gif .jpg .bmp .png - whatever your images are - DO NOT forget the leading DOT as this is appended to the filename later in the script.

Where you want the picture to be displayed, in a table cell, or anywhere on the page, you need to add the following lines.

<script language="JavaScript" type="text/JavaScript">
document.write('<img border="0" src="' + imagefilename + '" alt="weekly image">');
</script>

If you want the image to be a link, then use something like this

<script language="JavaScript" type="text/JavaScript">
document.write('<a href="put page or image to link to here"><img border="0" src="' + imagefilename + '" alt="weekly image"></a>');
</script>