Reputation: 83
I want an image on my website that changes every hour, when its 8AM I want image1.png
if it is 9AM I want image2.png
if it is 10AM I want image1.png
again and this for all hours.
All the hours you can divide by 2 have to be image1.png
, else it has to be image2.png
.
I don't know how to do this :S Can someone help?
I tried something like this:
<?php
date_default_timezone_set("Netherlands/Amsterdam");
$now = date('G');
if ($now > 7 && $now < 20) {
$day=1;
} else {
$day =0;
}
?>
if ($day=1){
echo 'day!';
}
else
{
echo 'night :(';
}
?>
But it also dont echo day or night.
Upvotes: 1
Views: 2274
Reputation:
Okay, English is not my Mother-tongue, but your intention is difficult to comprehend.
All the hours you can divide by 2 have to be image1.png, else it has to be image2.png. I don't know how to do this :S Can someone help?
You say you want to change the picture every hour, so if the hour is even (2,4,6,8, ... 24/0) you want picture1 displayed and else you want to show picture2.
But going after your code example you only check if it's day or night oO
if ($now > 7 && $now < 20) {
$day=1;
} else {
$day =0;
}
?>
if ($day=1){
echo 'day!';
}
else
{
echo 'night :(';
}
So what exactly do you want to achieve?
For the former just try to work with modulo, something like:
$now = Date('G');
if(($now % 2) == 1)
{
echo "$now is odd, so display picture2";
} else {
echo "$now is even, so display picture1";
}
if you want both, just combine them :) No need for 24 if-statements, modulo is what you want (http://www.php.net/manual/en/internals2.opcodes.mod.php)
If you want 24 different pictures (which I couldn't read out of your question), then go for another solution, like the one mentioned below mine ;)
I'm sorry if I got you wrong!
Upvotes: 0
Reputation: 1083
Here is a simple code that solves your questions, the image name should be the hour of the day (24 hour format), for example 1.jpg, 2.jpg etc...
<html>
<head>
</head>
<body>
<?php
date_default_timezone_set("Netherlands/Amsterdam");
$now = date('G');
$imageSrc = "images/" . now . ".jpg";
echo '<img src="' . $imageSrc . '" alt="" />';
?>
</body>
</html>
Upvotes: 1
Reputation: 1
If you want to achieve that in real time then you can use polling method in which a function will be executed after every 1hour to change the image dynamically
setTimeout(function(){/* javascript function which will change the image */},60000);
Check the site below-
Hope it will help.
Cheers!
Upvotes: -1
Reputation: 12841
$now = date('G');
if ($now > 7 && $now < 20) {
$hr = $now - 7;
$pic = "image{$hr}.png";
} else {
$pic = "image1.png";
}
echo "<img src='$pic'>";
Upvotes: 0
Reputation: 3417
Try this
if ($day==1){
echo 'day!';
}
else
{
echo 'night :(';
}
Using "=" means You are not checking value, but assigning
Upvotes: 1