Need help with PHP Time of Day Image Change script...

mitch.james31

New Member
Messages
2
Reaction score
0
Points
0
Hi there...

I am trying to use a PHP script to make a different image show depending on what time it is.

PHP:
<?php
date_default_timezone_set('UTC');

$time = date("H");

function changebg() {
if ($time = 01) {echo "images/headernight.gif";}
elseif ($time = 02) {echo "images/headernight.gif";}
elseif ($time = 03) {echo "images/headernight.gif";}
elseif ($time = 04) {echo "images/headernight.gif";}
elseif ($time = 05) {echo "images/headernight.gif";}
elseif ($time = 06) {echo "images/headerday.gif";}
elseif ($time = 07) {echo "images/headerday.gif";}
elseif ($time = 08) {echo "images/headerday.gif";}

//etc.. through to $time = 23

else {echo "images/headerday.gif";}
}
?>

//and then in the HTML body...

<td height="175" style="background: url(<?php changebg();?>)">&nbsp;</td>
For some reason, It only ever displays the image for if $time = 1...

I'm pretty new to PHP, and I have spent a few hours trying to get it to work with no success :confused: ... Has anybody got any ideas or solutions?

Many thanks,
mitch.james
 

AngusThermopyle

Active Member
Messages
319
Reaction score
52
Points
28
== double equality sign is test for equality.
= single equality sign is assignment


($time == '01') tests tests to see if the value of $time is '01'
($time = '01') assigns the value '01' to $time. It does not test it. The value of the expression is what is assigned, ie, the string '01' . the string '01' is true. So, the first test always is true, no matter what the original value of $time is.

You also ignored the value '00' (ie between midnight and one)

You have to use quotes, since what you are testing are strings, not numbers.
If you want to use numbers, use:
$localtime = localtime();
$time = $localtime[ 2 ];
 
Last edited:

lemon-tree

x10 Minion
Community Support
Messages
1,420
Reaction score
46
Points
48
Just something to bear in mind, this won't work across timezones and will therefore show the wrong images for most people around the world due to the differences in their time relative to the server's. The best way to avoid this issue is to assign this task to Javascript, this allows you to use the exact time set on the user's computer and therefore set the relevant image regardless of what region they are in.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Leading zeros cause numbers to be interpreted in octal (if possible). This won't be an issue in the example code since oct 0X == dec X, for X < 8. It will cause problems other times, so be careful.

If you find yourself writing a sequence of if statements, you should generally rewrite it using a switch or an array. In this particular case, a single if statement works best.

PHP:
if ($time <= 5 || $time >= 19) {
    echo "images/headernight.gif";
} else {
    echo "images/headerday.gif";
}



// with a switch
switch ($time) {
  case 0: // FALLTHRU
  case 1: // FALLTHRU
  case 2: // FALLTHRU
  case 3: // FALLTHRU
  case 4: // FALLTHRU
  case 5:
    echo "images/headernight.gif";

  default:
    echo "images/headerday.gif";
}



// with an array
$imgs = array(
  // 0-5
    "images/headernight.gif", "images/headernight.gif", "images/headernight.gif",
    "images/headernight.gif", "images/headernight.gif", "images/headernight.gif",
  // 6-19
     "images/headerday.gif", ...
);
echo $imgs[(int)$time];

JS:
Code:
hour = (new Date()).getHours();
if (hour <= 5 || 19 <= hours) {
  document.getElementById('sunIndicator').src = 'images/headernight.gif';
} else {
  document.getElementById('sunIndicator').src = 'images/headerday.gif';
}

Hopefully, you know better than to use a table based layout and that <td> element denotes tabular data. Even so, an <img> element is more appropriate than the background, from the look of things.

You have to use quotes, since what you are testing are strings, not numbers.
PHP will juggle strings to numbers when comparing with numbers, as described in comparison operators. However, using strings for comparison values will prevent problems with octal numbers I mentioned above.
 
Last edited:

mitch.james31

New Member
Messages
2
Reaction score
0
Points
0
Thanks for all the help guys... Its cleared things up a lot for me.

Although I feel a complete noob now :tongue:

The original code was from a tutorials website, but never worked... I had tried changing bits (eg. putting quotes around the numbers, double == etc.) but it never seemed to give me the right result.

I did see another piece of code that used a switch, which looked tidier, but I went for the 'if, elseif else' one as it was easier for me to understand.

PHP:
if ($time <= 5 || $time >= 19) { 
    echo "images/headernight.gif"; 
} else { 
    echo "images/headerday.gif"; 
}
As soon as I saw the single if statement I had a doh moment, lol.

I had also looked into JS, and got a script; but couldn't see how i could call the image up within the <td> element...

I suppose the way forward is to scrap table based layouts as has been mentioned; but I'm still learning - mistakes like these all help the process :smile:

Thanks again for all the helpful advise!
mitch.james
 
Top