Page 1 of 3 123 LastLast
Results 1 to 10 of 27

Thread: Display different image each hour of the day (based on server time)

  1. #1
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Display different image each hour of the day (based on server time)

    Hi All,
    I'm looking for a script that will function for a radio station website. I'd like it to be able to display a specific image during specific times of day. The times need to be based on server time, not the viewer's PC clock. I've found scripts that come close to what i'm looking for, but they either only display text OR they are based on the viewer's PC clock instead of server time.

    Example:
    From 8am - 12 Noon, display the title of the show AND the host's picture.

    From 12 Noon - 3:30pm, display the title of the next show AND the host's picture.

    ...and so on.


    The schedule will likely be the same Monday thru Friday, but Saturday and Sunday will have a different schedule.


    Any help would be appreciated!

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    This would best be done with PHP, or another server side code, so it can get info from a database, or at the very least, get the server's time. Javascript wouldn't be able to do as much and would be less compatible.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi djr33. I'm hoping for a PHP solution as well. Have you ever seen anything similar to what I'm asking for?

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Really, isn't all that complex.

    Here's the code--
    PHP Code:
    <?php
    $h 
    date('G'); //set variable $h to the hour of the day
    //G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.

    if ($h 7$img 'fish.jpg';
    else if (
    $h 20$img 'frogs.jpg';
    else 
    $img 'potatoes.jpg';

    //if it's before 7am, use fish image
    //if not and it's before 8pm, use frogs
    //otherwise, potatoes
    ?>

    The html for the page goes here...
    <html>
    ...
    <body>
    ...
    <img src="<?php echo $img?>">
    ...
    </html>
    Very simple example, but that's the idea behind it. Depends how complex your setup if and what you want to do with it. Easy with php.


    Using a database for this if you want to store all of the different things in there is a good idea, so just look into how PHP works with MySQL. Note that you need both installed on your server.
    Here's a good tutorial to get you started--
    http://php-mysql-tutorial.com
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks! For some of the "if" statments, I'd like to add a check to see what the day of the week is as well. Is my modification below correct?

    Code:
    <?php
    $h = date('G'); //set variable $h to the hour of the day
    $d = date('w'); //set variable $d to the day of the week.
    //G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
    
    if ($h < 7) && ($d == 0) $img = 'fish.jpg';
    else if ($h < 20) && ($d == 0) $img = 'frogs.jpg';
    else $img = 'potatoes.jpg';
    
    //if it's before 7am on Sunday, use fish image
    //if not and it's before 8pm on Sunday, use frogs
    //otherwise, potatoes
    ?>
    
    The html for the page goes here...
    <html>
    ...
    <body>
    ...
    <img src="<?php echo $img; ?>">
    ...
    </html>

    Thanks again!

  6. #6
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    It works after you change a few items. The errors are in red on the first code, and the second you will see the corrections.

    original:

    PHP Code:
    <?php
    $h 
    date('G'); //set variable $h to the hour of the day
    $d date('w'); //set variable $d to the day of the week.
    //G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.

    if ($h 7[COLOR="Red"])[/COLOR] && [COLOR="Red"]([/COLOR]$d == 0$img 'fish.jpg';
    else if (
    $h 20[COLOR="Red"])[/COLOR] && [COLOR="Red"]([/COLOR]$d == 0$img 'frogs.jpg';
    else 
    $img 'potatoes.jpg';

    //if it's before 7am on Sunday, use fish image
    //if not and it's before 8pm on Sunday, use frogs
    //otherwise, potatoes
    ?>

    The html for the page goes here...
    <html>
    ...
    <body>
    ...
    <img src="<?php echo $img?>">
    ...
    </html>
    corrected:

    PHP Code:
    <?php
    $h 
    date('G'); //set variable $h to the hour of the day
    $d date('w'); //set variable $d to the day of the week.
    //G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.

    if ($h && $d == 0$img 'fish.jpg';
    else if (
    $h 20 && $d == 0$img 'frogs.jpg';
    else 
    $img 'potatoes.jpg';

    //if it's before 7am on Sunday, use fish image
    //if not and it's before 8pm on Sunday, use frogs
    //otherwise, potatoes
    ?>

    The html for the page goes here...
    <html>
    ...
    <body>
    ...
    <img src="<?php echo $img?>">
    ...
    </html>
    The corrections I have done were to take out the extra parenthesis "( )" (not sure on spelling). In the if - else statements you only need an opening and closing set, nothing between the && (and) or || (or). Other than that, it works. I have tested it on my home server and was able to change the times and whatnot and it works as expected.

    Let me know if you need any more help.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  7. #7
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks! I'll test it out on my server and let you know if I have any trouble.

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Glad it's working.

    The info for date() is here-- http://www.php.net/manual/en/function.date.php but it looks like you've found it, considering you added the day of the week line.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Dec 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi Everyone,
    Thanks for all your help on this one. I succesfully got this working on the new site I'm creating. I needed to display a different image for each radio host at different points throughout the day, each day of the week. I also had to include a 2 hour offset from the server's time to match my time zone. Here's what my final code looks like.

    Hopfully this is helpfule to others looking to do the same thing.

    Regards,
    Torry

    PHP Code:
    <?php
    $h 
    date('G'); //set variable $h to the hour of the day
    $d date('w'); //set variable $d to the day of the week.
    $year date('Y'); //set variable $year to the current year
    //G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
    // Adjust 2 hour offset for MST below.
    $h $h-2;

    // MONDAY SCHEDULE
    if ($d == && $h >= && $h 4$img 'img/hosts/petem.jpg';
    else if (
    $d == && $h >= && $h 8$img 'img/hosts/angelaa.jpg';
    else if (
    $d == && $h >= && $h 12$img 'img/hosts/shonw.jpg';
    else if (
    $d == && $h >= 12 && $h 13$img 'img/hosts/pottsie.jpg';
    else if (
    $d == && $h >= 13 && $h 15$img 'img/hosts/mikef.jpg';
    else if (
    $d == && $h >= 15 && $h 19$img 'img/hosts/lizzy.jpg';
    else if (
    $d == && $h >= 19$img 'img/hosts/danic.jpg';
    else if (
    $d == && $h 0$img 'img/hosts/danic.jpg';

    // TUESDAY SCHEDULE
    if ($d == && $h >= && $h 4$img 'img/hosts/petem.jpg';
    else if (
    $d == && $h >= && $h 8$img 'img/hosts/angelaa.jpg';
    else if (
    $d == && $h >= && $h 12$img 'img/hosts/shonw.jpg';
    else if (
    $d == && $h >= 12 && $h 13$img 'img/hosts/pottsie.jpg';
    else if (
    $d == && $h >= 13 && $h 15$img 'img/hosts/mikef.jpg';
    else if (
    $d == && $h >= 15 && $h 17$img 'img/hosts/lizzy.jpg';
    else if (
    $d == && $h >= 17 && $h 20$img 'img/hosts/westmar.jpg';
    else if (
    $d == && $h >= 20$img 'img/hosts/danic.jpg';
    else if (
    $d == && $h 0$img 'img/hosts/danic.jpg';

    // WEDNESDAY SCHEDULE
    if ($d == && $h >= && $h 4$img 'img/hosts/petem.jpg';
    else if (
    $d == && $h >= && $h 8$img 'img/hosts/angelaa.jpg';
    else if (
    $d == && $h >= && $h 12$img 'img/hosts/shonw.jpg';
    else if (
    $d == && $h >= 12 && $h 13$img 'img/hosts/pottsie.jpg';
    else if (
    $d == && $h >= 13 && $h 15$img 'img/hosts/mikef.jpg';
    else if (
    $d == && $h >= 15 && $h 19$img 'img/hosts/lizzy.jpg';
    else if (
    $d == && $h >= 19$img 'img/hosts/danic.jpg';
    else if (
    $d == && $h 0$img 'img/hosts/danic.jpg';

    // THURSDAY SCHEDULE
    if ($d == && $h >= && $h 4$img 'img/hosts/petem.jpg';
    else if (
    $d == && $h >= && $h 8$img 'img/hosts/angelaa.jpg';
    else if (
    $d == && $h >= && $h 12$img 'img/hosts/shonw.jpg';
    else if (
    $d == && $h >= 12 && $h 13$img 'img/hosts/pottsie.jpg';
    else if (
    $d == && $h >= 13 && $h 15$img 'img/hosts/mikef.jpg';
    else if (
    $d == && $h >= 15 && $h 19$img 'img/hosts/lizzy.jpg';
    else if (
    $d == && $h >= 19$img 'img/hosts/danic.jpg';
    else if (
    $d == && $h 0$img 'img/hosts/danic.jpg';

    // FRIDAY SCHEDULE
    if ($d == && $h >= && $h 4$img 'img/hosts/petem.jpg';
    else if (
    $d == && $h >= && $h 8$img 'img/hosts/angelaa.jpg';
    else if (
    $d == && $h >= && $h 10$img 'img/hosts/shonw.jpg';
    else if (
    $d == && $h >= 10 && $h 12$img 'img/hosts/patm.jpg';
    else if (
    $d == && $h >= 12 && $h 13$img 'img/hosts/pottsie.jpg';
    else if (
    $d == && $h >= 13 && $h 15$img 'img/hosts/edp.jpg';
    else if (
    $d == && $h >= 15 && $h 18$img 'img/hosts/lizzy.jpg';
    else if (
    $d == && $h >= 18 && $h 20$img 'img/hosts/jeremyb.jpg';
    else if (
    $d == && $h >= 20 && $h 22$img 'img/hosts/exfyl.jpg';
    else if (
    $d == && $h >= 22$img 'img/hosts/stickyb.jpg';
    else if (
    $d == && $h 0$img 'img/hosts/stickyb.jpg';

    // SATURDAY SCHEDULE
    else if ($d == && $h >= && $h 4$img 'img/hosts/techtronic.jpg';
    else if (
    $d == && $h >= && $h 5$img 'img/hosts/pmw.jpg';
    else if (
    $d == && $h >= && $h 8$img 'img/hosts/geoffh.jpg';
    else if (
    $d == && $h >= && $h 9$img 'img/hosts/tomf.jpg';
    else if (
    $d == && $h >= && $h 10$img 'img/hosts/jimmyj.jpg';
    else if (
    $d == && $h >= 10 && $h 11$img 'img/hosts/jasonr.jpg';
    else if (
    $d == && $h >= 11 && $h 12$img 'img/hosts/hollyk.jpg';
    else if (
    $d == && $h >= 12 && $h 13$img 'img/hosts/tomt.jpg';
    else if (
    $d == && $h >= 13 && $h 14$img 'img/hosts/seanf.jpg';
    else if (
    $d == && $h >= 14 && $h 15$img 'img/hosts/nutmeg.jpg';
    else if (
    $d == && $h >= 15 && $h 17$img 'img/hosts/aaron_jenny.jpg';
    else if (
    $d == && $h >= 17 && $h 19$img 'img/hosts/rayg_adrians.jpg';
    else if (
    $d == && $h >= 19 && $h 22$img 'img/hosts/mattb.jpg';
    else if (
    $d == && $h >= 22$img 'img/hosts/hairballj.jpg';
    else if (
    $d == && $h 0$img 'img/hosts/hairballj.jpg';

    // SATURDAY SCHEDULE
    else if ($d == && $h >= && $h 2$img 'img/hosts/darrelm.jpg';
    else if (
    $d == && $h >= && $h 4$img 'img/hosts/techtronic.jpg';
    else if (
    $d == && $h >= && $h 5$img 'img/hosts/bigjon.jpg';
    else if (
    $d == && $h >= && $h 6$img 'img/hosts/joebear.jpg';
    else if (
    $d == && $h >= && $h 8$img 'img/hosts/russh.jpg';
    else if (
    $d == && $h >= && $h 9$img 'img/hosts/ronk.jpg';
    else if (
    $d == && $h >= && $h 10$img 'img/hosts/rockpoint.jpg';
    else if (
    $d == && $h >= 10 && $h 11$img 'img/hosts/churchatqc.jpg';
    else if (
    $d == && $h >= 11 && $h 12$img 'img/hosts/desertcf.jpg';
    else if (
    $d == && $h >= 12 && $h 16$img 'img/hosts/kristenm.jpg';
    else if (
    $d == && $h >= 16 && $h 17$img 'img/hosts/cdogg.jpg';
    else if (
    $d == && $h >= 17 && $h 18$img 'img/hosts/snarf_daff.jpg';
    else if (
    $d == && $h >= 18 && $h 19$img 'img/hosts/sonicsociety.jpg';
    else if (
    $d == && $h >= 19 && $h 21$img 'img/hosts/jscott.jpg';
    else if (
    $d == && $h >= 21$img 'img/hosts/ghostlytalk.jpg';
    else if (
    $d == && $h 0$img 'img/hosts/ghostlytalk.jpg';
    ?>

  10. #10
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Glad to here that it is working for you. Also, thanks for posting the above code, I'm sure it may come in handy for some of the visitors to these forums.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •