+ Reply to Thread
Results 1 to 5 of 5

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

  1. #1
    mitch.james31 is offline x10Hosting Member mitch.james31 is an unknown quantity at this point
    Join Date
    Aug 2010
    Posts
    2

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

    Hi there...

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

    PHP Code:
    <?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 ... Has anybody got any ideas or solutions?

    Many thanks,
    mitch.james

  2. #2
    AngusThermopyle is offline x10Hosting Member AngusThermopyle is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    84

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

    == 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 by AngusThermopyle; 08-03-2010 at 08:04 PM.

  3. #3
    lemon-tree's Avatar
    lemon-tree is offline x10 Minion lemon-tree has a spectacular aura about
    Join Date
    Nov 2007
    Posts
    1,420

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

    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.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

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

    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 Code:
    if ($time <= || $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.

    Quote Originally Posted by c.solomon View Post
    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 by misson; 08-03-2010 at 10:02 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    mitch.james31 is offline x10Hosting Member mitch.james31 is an unknown quantity at this point
    Join Date
    Aug 2010
    Posts
    2

    Red face Re: Need help with PHP Time of Day Image Change script...

    Thanks for all the help guys... Its cleared things up a lot for me.

    Although I feel a complete noob now

    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 Code:
    if ($time <= || $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

    Thanks again for all the helpful advise!
    mitch.james

+ Reply to Thread

Similar Threads

  1. PHP Dynamic Image Script error.
    By Blazer9131 in forum Programming Help
    Replies: 10
    Last Post: 08-08-2008, 03:24 PM
  2. Image Rating Script (Hot or Not)
    By gasikest in forum Scripts & 3rd Party Apps
    Replies: 3
    Last Post: 01-02-2008, 10:39 AM
  3. resize more than 1 image at the time?
    By jptosso in forum Programming Help
    Replies: 7
    Last Post: 12-10-2007, 06:33 PM
  4. 15 points for rotate image script
    By Josh in forum The Marketplace
    Replies: 3
    Last Post: 05-27-2006, 01:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers