i need a code for PHP changing image in every 2 hours at a time. Actually i have 6 images and i want to change these images automatically in every 2 hours. how can i achieve this anyone helps really appreciated.
If you have 6 images, for example
$img1 = '/image1.png';
$img2 = '/image2.png';
$img3 = '/image3.png';
$img4 = '/image4.png';
$img5 = '/image5.png';
$img6 = '/image6.png';
You can go making a if commands which would be most simple solution
if(date('g') == 1 || date('g') == 2){
$image = $img1;
}
if(date('g') == 3 || date('g') == 4){
$image = $img2;
}
if(date('g') == 5 || date('g') == 6){
$image = $img3;
}
...
Maybe not most effective code but should work like a quick solution
date('g')
is returning 12-hour format of an hour without leading zeros and it should be current time. You could also do something like this date('g', strtotime('now'))
but I don't think you need to
in php this could be acheived in a few simple lines of code, In fact, I put together a simple example that prints a different image based on what time it is - you should be easily able to adapt this to output a different image every hour.
$i = substr(time(), -1, 2 );
switch ($i) {
case 0:
$img = '<img src="zero.JPG" alt="img">';
break;
case 1:
$img = '<img src="one.JPG" alt="img">';
break;
case 2:
$img = '<img src="two.JPG" alt="img">';
break;
case 3:
$img = '<img src="three.JPG" alt="img">';
break;
case 4:
$img = '<img src="four.JPG" alt="img">';
break;
case 5:
$img = '<img src="five.JPG" alt="img">';
break;
case 6:
$img = '<img src="six.JPG" alt="img">';
break;
case 7:
$img = '<img src="seven.JPG" alt="img">';
break;
case 8:
$img = '<img src="eight.JPG" alt="img">';
break;
case 9:
$img = '<img src="nine.JPG" alt="img">';
break;
}
echo $img;
try this,
var i = 0; var path = new Array();
// LIST OF IMAGES
path[0] = "image.jpg";
path[1] = "image1.jpg";
path[2] = "image2.jpg";
path[3] = "image3.jpg";
path[4] = "image4.jpg";
path[5] = "image5.jpg";
function swapImage()
{
document.slide.src = path[i];
if(i < path.length - 1) i++;
else i = 0;
setTimeout("swapImage()",7200000);
}
window.addEventListener("load", swapImage);
</script>
<img height="230" name="slide" src="image.jpg" width="200" /></p>
</center></div></div>
<?php
header('Content-Type: image/png');
//add images acoordingly
$imageArr = array('image1.png','image2.png');
foreach($imageArr as $path){
$im = imagecreatefrompng($path);
imagepng($im);
imagedestroy($im);
sleep(2*60*60);
}
?>
is it the code you are looking for
<?php
// change every X hours
define('EVERY', 2);
// here we have the images
$images = [
'image1.png',
'image2.png',
'image3.png',
'image4.png',
'image5.png',
'image6.png'
];
// anonymous function to get the current Image
$imageFor = function ($hour) use ($images) {
$index = $hour / EVERY % count($images);
return $images[$index];
};
// output for all the hours
for ($i = 0; $i < 24; $i++)
echo "$i\t", $imageFor($i), PHP_EOL;
$currentHour = date('G');
echo "[$currentHour] Now showing Image: ", $imageFor($currentHour);
OUTPUT :
0 image1.png
1 image1.png
2 image2.png
3 image2.png
4 image3.png
5 image3.png
6 image4.png
7 image4.png
8 image5.png
9 image5.png
10 image6.png
11 image6.png
12 image1.png
13 image1.png
14 image2.png
15 image2.png
16 image3.png
17 image3.png
18 image4.png
19 image4.png
20 image5.png
21 image5.png
22 image6.png
23 image6.png
[8] Now showing Image: image5.png
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.