0

This is my code, which displays either an image, or message depending on the time:

<?php

$b = time();

$hour = date("g",$b);
$m = date ("A", $b);

if ($m == "AM")
{
if ($hour == 12)
{
echo "Good Evening!";
}
elseif ($hour < 4)
{
echo "Good Evening!";
}
elseif ($hour > 3)
{
echo "Good Morning!";
}
}

elseif ($m == "PM")
{
if ($hour == 12)
{
echo "Good Afternoon!";
}
elseif ($hour < 7)
{
echo "Good Afternoon!";
}
elseif ($hour > 6)
{
// echo "Good Evening!";
echo "<img src=\"http://www.mysitehere.com/test1.jpg\">";
}
}

?>

I'm using this as a script in my radio station site, displaying images at the times when presenters are on-air.

This is from schedule.php (a dynamically updated page which extracts info from the database):

Monday - Friday
12:00am John Doe
3:00am Joe Bloggs 1
5:30am Breakfast Show
9:00am Non Stop Music
10:00am Joe Blow
2:00pm Jane Doe
6:00pm Joe Q Public
10:00pm AN Other
Saturday
12:00am John Doe
6:00am Joe Public
10:00am Saturday Morning
12:30pm Non Stop Saturday Music
2:00pm AN Other
6:00pm Non Stop Music
10:00pm Love Songs
Sunday
12:00am Night Trax
4:00am AN Other
6:00am Placeholder1
10:00am Presenter
1:00pm Presenter2
4:00pm Chart Show
7:00pm Night Trax
9:00pm Love Songs

All the above shows have images associated with them; it's just getting them to display, especially for ones where they're at odd hours (e.g. 5:30am etc.)

Anyone able to help me with this, with how to get it right for images appearing at times like 5:30am etc? Your help would be much appreciated!

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by JRM
0

Here's a quick and dirty. Untested, but looks right to me...

<?php

function getTimeImage() {

	$hour = date('G'); // 24-hour hour without leading zeros
	$day = date('w'); // Day of the week, 0(Sunday) thru 6(Saturday)
	$min = date('i'); // minutes, no leading zeroes

	$monfri = array('johndoe.jpg','JoeBloggs.jpg','BreakfastShow.jpg','NonStopMusic.jpg','JoeBlow.jpg','JaneDoe.jpg','JoeQPublic.jpg','ANOther.jpg');

	$sat = array('JohnDoe.jpg','JoePublic.jpg','SaturdayMorning.jpg','NonStopSaturdayMusic.jpg','ANOther.jpg','NonStopMusic.jpg','LoveSongs.jpg');

	$sun = array('NightTrax.jpg','ANOther.jpg','Placeholder1.jpg','Presenter.jpg','Presenter2.jpg','ChartShow.jpg','NightTrax.jpg','LoveSongs.jpg');

	$nowtime = ($hour*60)+$min;

	if($day>0 && $day<6) {
		if($nowtime>=0 && $nowtime<180) { $image = $monfri[0]; }
		if($nowtime>=180 && $nowtime<330) { $image = $monfri[1]; }
		if($nowtime>=330 && $nowtime<540) { $image = $monfri[2]; }
		if($nowtime>=540 && $nowtime<600) { $image = $monfri[3]; }
		if($nowtime>=600 && $nowtime<840) { $image = $monfri[4]; }
		if($nowtime>=840 && $nowtime<1080) { $image = $monfri[5]; }
		if($nowtime>=1080 && $nowtime<1320) { $image = $monfri[6]; }
		if($nowtime>=1320 && $nowtime<1440) { $image = $monfri[7]; }
	}
	if($day==0) {
		if($nowtime>=0 && $nowtime<360) { $image = $sat[0]; }
		if($nowtime>=360 && $nowtime<600) { $image = $sat[1]; }
		if($nowtime>=600 && $nowtime<750) { $image = $sat[2]; }
		if($nowtime>=750 && $nowtime<840) { $image = $sat[3]; }
		if($nowtime>=840 && $nowtime<1080) { $image = $sat[4]; }
		if($nowtime>=1080 && $nowtime<1320) { $image = $sat[5]; }
		if($nowtime>=1320 && $nowtime<1440) { $image = $sat[6]; }
	}
	if($day==6) {
		if($nowtime>=0 && $nowtime<240) { $image = $sun[0]; }
		if($nowtime>=240 && $nowtime<360) { $image = $sun[1]; }
		if($nowtime>=360 && $nowtime<600) { $image = $sun[2]; }
		if($nowtime>=600 && $nowtime<780) { $image = $sun[3]; }
		if($nowtime>=780 && $nowtime<960) { $image = $sun[4]; }
		if($nowtime>=960 && $nowtime<1140) { $image = $sun[5]; }
		if($nowtime>=1140 && $nowtime<1260) { $image = $sun[6]; }
		if($nowtime>=1260 && $nowtime<1440) { $image = $sun[7]; }
	}
}
?>
	
<html>

...

<?php $var = getTimeImage(); ?>

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

...

</html>

My 2¢. Hope it helps.

0

It just works... but the images do not show, despite being in the same directory.

0

since you already have a table with the shows, times and images, use it to pull up the info. query the data base show time so that it will respond to a range of times. ie; if the show is an hour starting at 5pm use:

where time >= 5 AND time < 6

or what ever time format you are using.
As you you change the shows in the time slots, all of it updates on your site automatically.
The arrays will require hard coding future changes. make an admin page that a secretary can change.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.