1

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.

Jan Fabry
  • 7,221
  • 2
  • 36
  • 41
Chiel
  • 83
  • 10

5 Answers5

1

Try this

 if ($day==1){
    echo 'day!';
    }
    else
    {
    echo 'night :(';
    }

Using "=" means You are not checking value, but assigning

user1844933
  • 3,296
  • 2
  • 25
  • 42
  • Thanks, but how can i change it so the image changes every hour? Or do i have to make 24 if-statements? – Chiel Jan 04 '14 at 10:48
1

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>
MrByte
  • 1,083
  • 2
  • 11
  • 21
0
$now = date('G');

if ($now > 7 && $now < 20) {
    $hr = $now - 7;
    $pic = "image{$hr}.png";
} else {
    $pic = "image1.png";
}

echo "<img src='$pic'>";
MilMike
  • 12,571
  • 15
  • 65
  • 82
0

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!

-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-

JavaScript.setTimeout

Hope it will help.

Cheers!

Community
  • 1
  • 1
mvcnewbi
  • 1
  • 2