Reorder photos at midnight

I have a set of image ads that I want to change every day at midnight.

In principle, that one day it will be like

<img src="image1">
<img src="image2">
<img src="image3">
<img src="image4">

and the next day it will look like this

<img src="image4">
<img src="image1">
<img src="image2">
<img src="image3">

How could I accomplish this using javascript, jquery or php. Without worrying about which language I use, I just need to understand this. Thank..

+5
source share
4 answers

Try this http://jsfiddle.net/KQwf2/1/

HTML:

<img src="http://solarpanels2green.com/images/one.gif" title='1'>
<img src="http://solarpanels2green.com/images/two.gif" title='2'>
<img src="http://solarpanels2green.com/images/three.gif" title='3'>
<img src="http://solarpanels2green.com/images/four.gif" title='4'>

and js code

var all = $('img'), 
        shift = Math.floor(
          (new Date().getTime() - new Date().getTimezoneOffset() * 60000)
         / (24 * 3600 * 1000)) % all.length;

all.filter(':gt(' + (all.length - shift - 1) + ')')
   .insertBefore(all.first());

It calculates the MOD of dividing the number of days that have passed since midnight on January 1, 1970 by the number of elements in the image list, takes this number of images from the bottom of the list, and moves them forward of the list.

, .

+4

, php . , , , , , . php rand.

, , . - .

php non-DB:

$fullhash = md5(date("Ymd"));
$hash = $fullhash;
$countImages = 4; //or whatever the actual number of images you have is
$shownImages = array();
while ($countShown < $countImages) 
{
  $num = ord($hash); //get ascii value of first char of $hash
  $num = $num % $countImages; //convert the number to something that corresponds to an image
  if (!(in_array($num, $shownImages)))
  {
    echo "<img src='image" . $num . "'>";
    $shownImages[] = $num;
  }
  $hash = substr($hash,1);
  if (strlen($hash) == 0)
  {
    $fullhash = md5($fullhash); //generate a new hash in case the previous one did not catch all images
    $hash = $fullhash;
  }
}

. , . , .

+2

Here is one of PHP, which depends only on the time of the last modification of the set of images in this directory:

<?php

function cmp($a, $b){
  $atime = filemtime($a);
  $btime = filemtime($b);

  return $atime == $btime ? 0 : ($atime < $btime ? -1 : 1);
}

$paths = array();

if ($dh = opendir('images')){
  while (($path = readdir($dh)) !== false){
    if (substr($path, -4) == '.jpg'){
      array_push($paths, "images/$path");
    }
  }
}

$count = count($paths);
$offset = time() % $count;

usort($paths, 'cmp');

for ($i = 0; $i < $offset; $i++){
  $path = array_shift($paths);
  array_push($paths, $path);
}

?>

Then, where you need it on your page:

<?php foreach ($paths as $path): ?>
  <img src="<?php echo $path; ?>" ... />
<?php endforeach; ?>
+1
source

you can get time using javascript. then I would create an array, randomize the order and display your images

var d = new Date();
var time= d.getHours();
if(time>=24){
  //code here for randomizing
}

http://www.w3schools.com/jsref/jsref_gethours.asp
http://www.javascriptkit.com/javatutors/randomorder.shtml

0
source

All Articles