Helpful Information
 
 
Category: Looking for such a script or service
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! :D

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.

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

Really, isn't all that complex.

Here's the 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

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?


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

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


corrected:



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


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.

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

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.

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
$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 == 1 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
else if ($d == 1 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
else if ($d == 1 && $h >= 8 && $h < 12) $img = 'img/hosts/shonw.jpg';
else if ($d == 1 && $h >= 12 && $h < 13) $img = 'img/hosts/pottsie.jpg';
else if ($d == 1 && $h >= 13 && $h < 15) $img = 'img/hosts/mikef.jpg';
else if ($d == 1 && $h >= 15 && $h < 19) $img = 'img/hosts/lizzy.jpg';
else if ($d == 1 && $h >= 19) $img = 'img/hosts/danic.jpg';
else if ($d == 2 && $h < 0) $img = 'img/hosts/danic.jpg';

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

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

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

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

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

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

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.

Hi there, im using this php script and i want to add minutes on the script but i have a problem when the radio programs last more than an hour the script just doesnt show anything after that minute comes.


<?php
$hora = date('G'); // Horas de 0 a 23
$dia = date('w'); // Dias 0 (Domingo) a 6 (Sabado)
$minuto = date('i'); // Minutos de 00 a 59

$hora = $hora+1;
$minuto = $minuto+21;

// MARTES
if ($dia == 2 && $hora >= 0 && $hora < 4) $img = 'images/yaco.gif';
else if ($dia == 2 && $hora >= 4 && $hora < 8) $img = 'images/nancy.gif';
else if ($dia == 2 && $hora >= 8 && $hora < 12) $img = 'images/lucas.gif';
else if ($dia == 2 && $hora >= 12 && $hora < 13) $img = 'images/juan.gif';
else if ($dia == 2 && $hora >= 13 && $hora < 15) $img = 'images/henoch.gif';
else if ($dia == 2 && $hora >= 15 && $hora < 17) $img = 'images/gustavo.gif';
else if ($dia == 2 && $hora >= 17 && $hora <= 21 && $minuto <= 44) $img = 'images/edmundo.gif';
?>

<html>
<head></head>
<body>

Estas escuchando a...

<br><br>

<img src="<?php echo $img; ?>">

</body>
</html>

can you guys help me please? thanks in advance!

Hi,

This post is old but...

I just came across this script and it's almost exactly what I need, but I'm also interested in adding minutes as my schedule changes on the mid point of some hours.

If anyone can help, I would appreciate it greatly. I would even pay you a bit for you time.

Thanks.

would it be possible for this code to be made in to html or css? if so would someone please help me to do it as I dont know how.

HTML and CSS don't allow for conditional ("if") statements. No. You need Javascript, or a serverside programming language like PHP, ASP, CGI, etc.
(Of course any of those can be combined with HTML and CSS as needed to create the desired look.)

Hello thanks for your reply,

the problem I have is I can not upload PHP to my host, but JS etc I can, would it be possible for you to help me make this into a JS code by any chance?

I should clarify something: you can do this in Javascript, but you cannot base it on server time. It will be based on the user's time on their computer (even if it is wrong or in a different location). You might be able to use Javascript to make it relative to the right timezone (adjust based on the current timezone) if that information is available. I'm not sure.

But if you google "change image every hour javascript" you will find some information. Please do a search first.

If your specific question is about the server time, then, no, you must have a serverside language like PHP available. You can do everything else in Javascript, but not the server time.

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
$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 == 1 && $h >= 0 && $h < 4) $img = 'img/hosts/petem.jpg';
else if ($d == 1 && $h >= 4 && $h < 8) $img = 'img/hosts/angelaa.jpg';
else if ($d == 1 && $h >= 8 && $h < 12) $img = 'img/hosts/shonw.jpg';
else if ($d == 1 && $h >= 12 && $h < 13) $img = 'img/hosts/pottsie.jpg';
else if ($d == 1 && $h >= 13 && $h < 15) $img = 'img/hosts/mikef.jpg';
else if ($d == 1 && $h >= 15 && $h < 19) $img = 'img/hosts/lizzy.jpg';
else if ($d == 1 && $h >= 19) $img = 'img/hosts/danic.jpg';
else if ($d == 2 && $h < 0) $img = 'img/hosts/danic.jpg';

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

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

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

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

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

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



this covers what i would like it to do but im stuck on how to make it more dynamic?
id like it to run via mysql so that a form to edit all its details is possible on another page..
so i can select what dj at what time is playing.. or add another dj to the schedule:confused:

can any one please help?

Do you have much experience with php/MySQL?

If you have the basis for your HTML form, and have some experience with php/MySQL then this might be something that somebody here can guide you on.

However, if you have no experience and require somebody to code this for you as a custom application, it will probably need to be taken to the paid work requests forum : http://www.dynamicdrive.com/forums/forumdisplay.php?30-General-Paid-Work-Requests

Hi guys,

I was searching for a code to switch an image at certain interval of time using hour + minutes.

This code is for a radio so I need to change the image of each host at each hour+minute.

ie: 7.30 - 7.45 or 8.00 - 10.30

Can you guys help me on this?

This is the code i'm using:



<?php
$h = date('G'); //set variable $h to the hour of the day.
$m = date('i'); //set variable $m to the min of the hour.
$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;

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

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

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

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

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

// SATURDAY SCHEDULE
else if ($d == 6 && $h >= 4 && $h < 6) $img = 'images/horario/pmw.jpg';
else if ($d == 6 && $h >= 6 && $h < 7) $img = 'images/horario/geoffh.jpg';
else if ($d == 6 && $h >= 7 && $h < 7) $img = 'images/horario/tomf.jpg';
else if ($d == 6 && $h >= 7 && $h < 7) $img = 'images/horario/tomf.jpg';
else if ($d == 6 && $h >= 7 && $h < 9) $img = 'images/horario/jimmyj.jpg';
else if ($d == 6 && $h >= 9 && $h < 11) $img = 'images/horario/jasonr.jpg';
else if ($d == 6 && $h >= 11 && $h < 13) $img = 'images/horario/hollyk.jpg';
else if ($d == 6 && $h >= 13 && $h < 14) $img = 'images/horario/hollyk.png';
else if ($d == 6 && $h >= 14 && $h < 17) $img = 'images/horario/hollyk.png';
else if ($d == 6 && $h >= 17 && $h < 23) { $img = 'images/horario/byron.png'; $locutor = 'Byron'; $hora = '5:00 PM - 11:00 PM'; $text = 'ELECTRONIC MUSIC RADIO SHOW'; if(mb_strlen($text)>13) { $text=mb_substr($text, 0, 13); $text.='...'; } }
else if ($d == 6 && $h >= 23) $img = 'images/horario/hairballj.jpg';
else if ($d == 0 && $h < 0) $img = 'images/horario/hairballj.jpg';

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

echo'<div style="float: left; width: 90px"><img src="'.$img.'" height="80px" width="80px" style="border: 1px solid green" /></div><div style="float: left; width: 141px; height: 80px; margin-top: 10px; color: #FFFFFF; font-family: Tahoma; font-size: 15px;">'.$text.' <br \>'.$hora.'<br \> Con: '.$locutor.'</div>';
?>



This is the code. Not finished, I copied from last posts. Works fine just with hours but I wanna add minutes the code. I already put this $m = date('i'); //set variable $m to the min of the hour. at the beginning of the code, but now I don't know how to put it running.

Thanks in advance.

Beverleyh

Can you help me?

If you have a new question, please start a new thread. Don't interrupt or restart an old discussion with something new-- that's just confusing. (And this advice is intended to get you better help.)

If you have a new question, please start a new thread. Don't interrupt or restart an old discussion with something new-- that's just confusing. (And this advice is intended to get you better help.)


But my question is related to this topic... -.-'

I just wanna add minutes to the code, since only have days and hours.

Need to open a new topic for that?

Yes, if you need specific help for your own question that goes beyond this one, it is better to start a new thread. (You will also receive more attention that way, rather than in the old thread here.)
You can link back to this one as needed.

But since your question is related and might be simple to solve, let me see if this helps:
You can add the following code to all of the lines as needed:
&& $m>10
That means "and it is 10 minutes after the hour". Adjust as needed.

Does that solve the problem? If you need more help and we need to have a longer conversation, I do recommend a new thread.

Yes, if you need specific help for your own question that goes beyond this one, it is better to start a new thread. (You will also receive more attention that way, rather than in the old thread here.)
You can link back to this one as needed.

But since your question is related and might be simple to solve, let me see if this helps:
You can add the following code to all of the lines as needed:
&& $m>10
That means "and it is 10 minutes after the hour". Adjust as needed.

Does that solve the problem? If you need more help and we need to have a longer conversation, I do recommend a new thread.

Hey, thanks for ur reply.

This is what i'm using:


if ($d == 1 && $h >= 7 && $h < 7 && $m <= 20) { $img = 'images/horario/auto_system.png'; $locutor = 'Radio5'; $hora = '7:00 AM - 7:20 AM'; $text = 'Panorama'; if(mb_strlen($text)>13) { $text=mb_substr($text, 0, 13); $text.='...'; } }

else if ($d == 1 && $h >= 7 && $m <= 20 && $h < 8) { $img = 'images/horario/delbis_02.png'; $locutor = 'Delbis'; $hora = '7:20 AM - 8:00 AM'; $text = 'La Bendicion De La Maņana'; if(mb_strlen($text)>13) { $text=mb_substr($text, 0, 13); $text.='...'; } }

echo'<div style="float: left; width: 90px"><img src="'.$img.'" height="80px" width="80px" style="border: 1px solid green" /></div><div style="float: left; width: 141px; height: 80px; margin-top: 10px; color: #FFFFFF; font-family: Tahoma; font-size: 15px;">'.$text.' <br \>'.$hora.'<br \> Con: '.$locutor.'</div>';


How are read this functions?

If day 1 and hour >= 7 and hour < 7 and minute <= 20 ?

If day 1 and hour >= 7 and minute <=20 and hour < 8 ?

But it work for this 7.00 AM - 7.20 AM and 7:20 AM - 8:00 AM?

I don't have to group hour with minutes? with () or anything?

Thanks for your help. I think this is related and topic is old but very usefull.

It depends on the exactly logic you require. && is symmetrical and doesn't need to have any parentheses.

$d==1 && $h<12 && $m>10

That means "if the day is '1' and it's before 12 hours, and it's after 10 minutes..."
That describes ANY time after 10 minutes past the hour, so... 12:10am, 1:10am, ... 11:10am...

If you're looking for an exact time, use ==:
$d==1 && $h==12 && $m>10
That means 11:10am-11:59am

Thanks you helped a lot.

I last doubt, is this correct? I want to make 7:20 AM - 9:00 AM


else if ($d == 6 && $h == 7 && $m > 20 && $h < 9) { $img = 'images/horario/auto_system.png'; $locutor = 'Auto'; $hora = '7:20 AM - 9:00 AM'; $text = 'Musica Fin De Semana'; if(mb_strlen($text)>13) { $text=mb_substr($text, 0, 13); $text.='...'; } }

Thanks.

For a range like that you'll need to use some parentheses. There are a few ways to do that. One would be to convert the time to something like 0720 0900 and then just check if the number is between those points ($n>720&&$n<900). But you can do it the long way like this:
if ($d==6 && ($h==8|| ($h==7 && $m>20) ) )

Note that I'm doing this as exclusive-- the endpoints won't count. So it's from 7:21 to 8:59 inclusive. As needed, you can adjust the rest.

So you can use parentheses as needed. And ==, <, >, <=, <= for comparison. Then && for 'and' and || for 'or'. That should be everything you need.

As for the other method, the easiest way is probably to multiply like this:
$n = $d*10000 + $h*100 + $m;

That'll give you a number that is in the right sequence, although the actual way that the numbers add up won't make much sense (just like on a clock, it'll skip from 959 to 1000 etc.).










privacy (GDPR)