Display a random image same all day

September 8, 2007

You probably many times displayed images from a folder choosing one at random. That is known as a image rotator script. There are many available over the WWW, but very few of them ( if there is any) does rotate the images in a specific way: a picture is displayed for a whole day, and not a new one each time it loads.

So how can I achieve that?

Well is pretty simple the way it is. You just have to think the right way. Basically, we will read all files in the target directory and put them in an array. That’s probably what all of rotator script do anyways. So no need to that here.

Next, we’ll generate a random number that is between 0 and the size of the array, but this random number will be tied by the data ( day in particular).

So the whole thing is to generate that random number, same the whole day, each time the script runs. Here it is the snipped how it looks:

function set_seed()

  {

 	$time = time();

 	$today = mktime(0, 0, 0, (int)date("n", $time),(int)date("j", $time), (int)date("Y", $time));

 	srand($today / pi());

 	break;  }

Next, to generate a random number, that is same each time runs the whole day we just do this

set_seed();
$no=rand(0,100);

This sequence will generate a random number between 0-100 that will be same each time the script runs during one specific day.

 

Post a comment

Name (required)

Mail (will not be published) (required)

Website

*
To prove you're a person (not a spam script), type the security text shown in the picture. Click here to regenerate some new text.
Click to hear an audio file of the anti-spam word