Jump to content

PHP time based image swap issue


joomlavideos

Recommended Posts

Hi, I found this cool script that will allow me to change an image based on the time of day and the week.

 

Here is the script itself:

 

------------------

 

<?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 1 hour offset for CST below.

$h = $h-1;

 

// MONDAY SCHEDULE

if ($d == 1 && $h >= 16 && $h < 17) $img = 'images/onAir.gif';

else if ($d == 1 && $h >= 17 && $h < 16) $img = 'images/offAir.gif';

 

 

// TUESDAY SCHEDULE

if ($d == 2 && $h >= 16 && $h < 17) $img = 'images/onAir.gif';

else if ($d == 2 && $h >= 17 && $h < 16) $img = 'images/offAir.gif';

 

 

// WEDNESDAY SCHEDULE

if ($d == 3 && $h >= 16 && $h < 17) $img = 'images/onAir.gif';

else if ($d == 3 && $h >= 17 && $h < 16) $img = 'images/offAir.gif';

 

 

// THURSDAY SCHEDULE

if ($d == 4 && $h >= 16 && $h < 17) $img = 'images/onAir.gif';

else if ($d == 4 && $h >= 17 && $h < 16) $img = 'images/offAir.gif';

 

 

// FRIDAY SCHEDULE

if ($d == 5 && $h >= 16 && $h < 17) $img = 'images/onAir.gif';

else if ($d == 5 && $h >= 17 && $h < 16) $img = 'images/offAir.gif';

 

 

// SATURDAY SCHEDULE

else if ($d == 7 && $h >= 0 && $h < 24) $img = 'images/offAir.gif';

 

 

 

// SATURDAY SCHEDULE

else if ($d == 7 && $h >= 0 && $h < 24) $img = 'images/offAir.gif';

 

?>

 

basically this is for a radio show - and I have an image I want to display between 4-5pm CST that says "On the Air", and then display "Off the Air" any other time of the day.

 

My question is this: Being that the place where I want this image to load is in a DIV and in an include, where do I load this script? I tried to place the script in the div itself in the include, then I tried create a separate include with a php extension and insert it within the original include itself (nested - I know probably not a good idea), but I am not seeing anything in either case.

 

Would anyone have an idea on how I can modify this to accommodate my wishes?

 

Thanks!

 

JJ

Link to comment
Share on other sites

Hi joomlavideos,

Can you try this script. I have assumed that the radio will be offair on saturday and sunday. Regarding the time this script takes the server time so you might need to compare the server time and your local time.

<?php
$d=date("l");
$h=date("h");

//offline for saturday and sunday
if($d=="Saturday" or $d=="Sunday"){
   $img="images/offAir.gif"; //offline image.
}else{
   if($h >= 4 and $h < 5){
       $img="images/onAir.gif"; //for online image.
   }else{
       $img="images/offAir.gif";//offline after 5pm and before 4pm
   }
}
?>




Link to comment
Share on other sites

start with creating test file, say test.php - copy paste this script into it as it is - just to see if this script working.

Check also paths to the images - they should be in "images" folder as it stand in the script:

images/offAir.gif.

then use this script as include in any div as you were doing but again, check if paths to the images are correct or rather use absolute paths while testing.

Link to comment
Share on other sites

Hi joomlavideos,

Can you try this script. I have assumed that the radio will be offair on saturday and sunday. Regarding the time this script takes the server time so you might need to compare the server time and your local time.

<?php
$d=date("l");
$h=date("h");

//offline for saturday and sunday
if($d=="Saturday" or $d=="Sunday"){
   $img="images/offAir.gif"; //offline image.
}else{
   if($h >= 4 and $h < 5){
       $img="images/onAir.gif"; //for online image.
   }else{
       $img="images/offAir.gif";//offline after 5pm and before 4pm
   }
}
?>




 

 

Thanks for the reply. I inserted your script in the include and tested in the browser. But what I got was the code showing on the web page.

 

Am I supposed to place the actual PHP code somewhere else, and then use the <?=$img?>

in the div?

 

Thanks!

Link to comment
Share on other sites

 

<?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 1 hour offset for CST below.

$h = $h-1;

 

// MONDAY SCHEDULE

if ($d == 1 && $h >= 16 && $h < 17) $img = 'http://ourclient.info/WSS3/images/onAir.gif';

else if ($d == 1 && $h >= 17 && $h < 16) $img = 'http://ourclient.info/WSS3/images/offAir.gif';

 

 

// TUESDAY SCHEDULE

if ($d == 2 && $h >= 16 && $h < 17) $img = 'http://ourclient.info/WSS3/images/onAir.gif';

else if ($d == 2 && $h >= 17 && $h < 16) $img = 'http://ourclient.info/WSS3/images/offAir.gif';

 

 

// WEDNESDAY SCHEDULE

if ($d == 3 && $h >= 16 && $h < 17) $img = 'http://ourclient.info/WSS3/images/onAir.gif';

else if ($d == 3 && $h >= 17 && $h < 16) $img = 'http://ourclient.info/WSS3/images/offAir.gif';

 

 

// THURSDAY SCHEDULE

if ($d == 4 && $h >= 16 && $h < 17) $img = 'http://ourclient.info/WSS3/images/onAir.gif';

else if ($d == 4 && $h >= 17 && $h < 16) $img = 'http://ourclient.info/WSS3/images/offAir.gif';

 

 

// FRIDAY SCHEDULE

if ($d == 5 && $h >= 16 && $h < 17) $img = 'http://ourclient.info/WSS3/images/onAir.gif';

else if ($d == 5 && $h >= 17 && $h < 16) $img = 'http://ourclient.info/WSS3/images/offAir.gif';

 

 

// SATURDAY SCHEDULE

else if ($d == 6 && $h >= 0 && $h < 24) $img = 'http://ourclient.info/WSS3/images/offAir.gif';

 

 

 

// SUNDAY SCHEDULE

else if ($d == 7 && $h >= 0 && $h < 24) $img = 'http://ourclient.info/WSS3/images/offAir.gif';

 

?>

 

Link to comment
Share on other sites

so this is your working script :

 

<?php

$d=date("l");

$h=date("h");

 

//offline for saturday and sunday

if($d=="Saturday" or $d=="Sunday"){

$img="imagepath"; //offline image.

}else{

if($h >= 4 and $h

$img="imagepath"; //for online image.

}else{

$img="imagepath";//offline after 5pm and before 4pm

}

}

?>

<?php echo $img;?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...