Quantcast
Viewing all articles
Browse latest Browse all 2

How To Build A Dynamic “On Air Now” Banner Using PHP.

Recently I was looking for a way to change an image on a website according to a schedule. I found a pretty simple way to do this with a little bit of PHP and thought I would share. I’ll do my best to explain, but if you have any questions, feel free to post a comment below.

First, you’ll need to open your PHP tag:

<?php

Then set up the variables you’ll need for the schedule:

$m = date('i');
$hhmm = $h * 100 + $m; //create a variable called $m to define to the minute of the hour. 
$h = date('G'); //create a variable called $h to define to the hour of the day.
$d = date('w'); //create a variable called $d to the define to the day of the week.
$year = date('Y'); //create a variable called $year to define to the current year. 

As you can see, I’m using minutes, hours, days, and I’ve also included year for demonstration purposes. The next step might take some experimenting. You’ll need to find the UTC offset that your sever uses. This isn’t necessarily where your hosting company is located. I know mine is UTC-07:00, so I’ve told the script to use that timezone when determining the schedule:

$h = $h-7; 

So, once that’s done…we’ll set up the first day. In the very first line below, we’re basically saying, if the day ($d) is Monday (1), and the hour ($h) is greater than 0 and less that 1, display this image (images/pic1.jpg’).

if ($d == 1 && $h >= 0 && $h < 1) { $img = 'images/hosts/image1.jpg'; }

The physical image will be displayed whereever you add:

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

Enjoy!

        
<?php
$m = date('i');
$hhmm = $h * 100 + $m; //create a variable called $m to define to the minute of the hour. 
$h = date('G'); //create a variable called $h to define to the hour of the day.
$d = date('w'); //create a variable called $d to the define to the day of the week.
$year = date('Y'); //create a variable called $year to define to the current year.
// Note that the hours are in a 24 hour format.
// Adjust the UTC offset to the timezone your SERVER uses. I'll be demonstrating
with UTC-07:00.
$h = $h-7;
// MONDAY SCHEDULE if ($d == 1 && $h >= 0 && $h < 1) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 1 && $hhmm >= 0100 && $hhmm < 0542) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 1 && $hhmm >= 0542 && $hhmm < 0600) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 1 && $h >= 6 && $h < 7) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 1 && $h >= 7 && $h < 8) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 1 && $h >= 8 && $h < 9) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 1 && $h >= 9 && $h < 10) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 1 && $h >= 10 && $h < 11) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 1 && $h >= 11 && $h < 12) { $img = 'images/hosts/image5.jpg'; }
else if ($d == 1 && $h >= 12 && $h < 13) { $img = 'images/hosts/image5.jpg'; }
else if ($d == 1 && $h >= 13 && $h < 14) { $img = 'images/hosts/image6.jpg'; }
else if ($d == 1 && $h >= 14 && $h < 15) { $img = 'images/hosts/image6.jpg'; }
else if ($d == 1 && $h >= 15 && $h < 16) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 1 && $hhmm >= 1600 && $hhmm < 1730) { $img = 'images/hosts/image1.jpg';}
else if ($d == 1 && $hhmm >= 1730 && $hhmm < 1930) { $img = 'images/hosts/image7.jpg'; }
else if ($d == 1 && $hhmm >= 1930 && $hhmm < 2000) { $img = 'images/hosts/image8.jpg'; }
else if ($d == 1 && $h >= 20 && $h < 22) { $img = 'images/hosts/image9.jpg'; }
else if ($d == 1 && $h >= 22 && $h < 23) { $img = 'images/hosts/image10.jpg'; }
else if ($d == 1 && $h >= 23) { $img = 'images/hosts/image10.jpg'; }
// TUESDAY SCHEDULE
if ($d == 2 && $h >= 0 && $h < 1) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 2 && $hhmm >= 0100 && $hhmm < 0542) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 2 && $hhmm >= 0542 && $hhmm < 0600) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 2 && $h >= 6 && $h < 7) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 2 && $h >= 7 && $h < 8) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 2 && $h >= 8 && $h < 9) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 2 && $h >= 9 && $h < 10) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 2 && $h >= 10 && $h < 11) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 2 && $h >= 11 && $h < 12) { $img = 'images/hosts/image5.jpg'; }
else if ($d == 2 && $h >= 12 && $h < 13) { $img = 'images/hosts/image5.jpg'; }
else if ($d == 2 && $h >= 13 && $h < 14) { $img = 'images/hosts/image6.jpg'; }
else if ($d == 2 && $h >= 14 && $h < 15) { $img = 'images/hosts/image6.jpg'; }
else if ($d == 2 && $h >= 15 && $h < 16) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 2 && $hhmm >= 1600 && $hhmm < 1730) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 2 && $hhmm >= 1730 && $hhmm < 1930) { $img = 'images/hosts/image7.jpg'; }
else if ($d == 2 && $hhmm >= 1930 && $hhmm < 2000) { $img = 'images/hosts/image8.jpg'; }
else if ($d == 2 && $h >= 20 && $h < 22) { $img = 'images/hosts/image9.jpg'; }
else if ($d == 2 && $h >= 22 && $h < 23) { $img = 'images/hosts/image10.jpg'; }
else if ($d == 2 && $h >= 23) { $img = 'images/hosts/image10.jpg'; }
// WEDNESDAY SCHEDULE
if ($d == 3 && $h >= 0 && $h < 1) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 3 && $hhmm >= 0100 && $hhmm < 0542) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 3 && $hhmm >= 0542 && $hhmm < 0600) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 3 && $h >= 6 && $h < 7) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 3 && $h >= 7 && $h < 8) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 3 && $h >= 8 && $h < 9) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 3 && $h >= 9 && $h < 10) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 3 && $h >= 10 && $h < 11) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 3 && $h >= 11 && $h < 12) { $img = 'images/hosts/image5.jpg'; }
else if ($d == 3 && $h >= 12 && $h < 13) { $img = 'images/hosts/image5.jpg'; }
else if ($d == 3 && $h >= 13 && $h < 14) { $img = 'images/hosts/image6.jpg'; }
else if ($d == 3 && $h >= 14 && $h < 15) { $img = 'images/hosts/image6.jpg'; }
else if ($d == 3 && $h >= 15 && $h < 16) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 3 && $hhmm >= 1600 && $hhmm < 1730) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 3 && $hhmm >= 1730 && $hhmm < 1930) { $img = 'images/hosts/image7.jpg'; }
else if ($d == 3 && $hhmm >= 1930 && $hhmm < 2000) { $img = 'images/hosts/image8.jpg'; }
else if ($d == 3 && $h >= 20 && $h < 22) { $img = 'images/hosts/image9.jpg'; }
else if ($d == 3 && $h >= 22 && $h < 23) { $img = 'images/hosts/image10.jpg'; }
else if ($d == 3 && $h >= 23) { $img = 'images/hosts/image10.jpg'; }
// THURSDAY SCHEDULE
if ($d == 4 && $h >= 0 && $h < 1) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 4 && $hhmm >= 0100 && $hhmm < 0542) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 4 && $hhmm >= 0542 && $hhmm < 0600) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 4 && $h >= 6 && $h < 7) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 4 && $h >= 7 && $h < 8) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 4 && $h >= 8 && $h < 9) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 4 && $h >= 9 && $h < 10) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 4 && $h >= 10 && $h < 11) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 4 && $h >= 11 && $h < 12) { $img = 'images/hosts/image5.jpg'; }
else if ($d == 4 && $h >= 12 && $h < 13) { $img = 'images/hosts/image5.jpg'; }
else if ($d == 4 && $h >= 13 && $h < 14) { $img = 'images/hosts/image6.jpg'; }
else if ($d == 4 && $h >= 14 && $h < 15) { $img = 'images/hosts/image6.jpg'; }
else if ($d == 4 && $h >= 15 && $h < 16) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 4 && $hhmm >= 1600 && $hhmm < 1730) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 4 && $hhmm >= 1730 && $hhmm < 1930) { $img = 'images/hosts/image7.jpg'; }
else if ($d == 4 && $hhmm >= 1930 && $hhmm < 2000) { $img = 'images/hosts/image8.jpg'; }
else if ($d == 4 && $h >= 20 && $h < 22) { $img = 'images/hosts/image9.jpg'; }
else if ($d == 4 && $h >= 22 && $h < 23) { $img = 'images/hosts/image10.jpg'; }
else if ($d == 4 && $h >= 23) { $img = 'images/hosts/image10.jpg'; }
// FRIDAY SCHEDULE
if ($d == 5 && $h >= 0 && $h < 1) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 5 && $hhmm >= 100 && $hhmm < 542) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 5 && $hhmm >= 542 && $hhmm < 600) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 5 && $h >= 6 && $h < 7) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 5 && $h >= 7 && $h < 8) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 5 && $h >= 8 && $h < 9) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 5 && $h >= 9 && $h < 10) { $img = 'images/hosts/image4.jpg'; }
else if ($d == 5 && $h >= 10 && $h < 11) { $img = 'images/hosts/image5.jpg'; }
else if ($d == 5 && $h >= 11 && $h < 12) { $img = 'images/hosts/image5.jpg'; }
else if ($d == 5 && $h >= 12 && $h < 13) { $img = 'images/hosts/image16.jpg'; }
else if ($d == 5 && $h >= 13 && $h < 14) { $img = 'images/hosts/image6.jpg'; }
else if ($d == 5 && $h >= 14 && $h < 15) { $img = 'images/hosts/image6.jpg'; }
else if ($d == 5 && $h >= 15 && $h < 16) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 5 && $hhmm >= 1600 && $hhmm < 1730) { $img = 'images/hosts/image1.jpg'; }
else if ($d == 5 && $hhmm >= 1730 && $hhmm < 1930) { $img = 'images/hosts/image7.jpg'; }
else if ($d == 5 && $hhmm >= 1930 && $hhmm < 2000) { $img = 'images/hosts/image8.jpg'; }
else if ($d == 5 && $h >= 20 && $h < 22) { $img = 'images/hosts/image9.jpg'; }
else if ($d == 5 && $h >= 22 && $h < 23) { $img = 'images/hosts/image10.jpg'; }
else if ($d == 5 && $h >= 23) { $img = 'images/hosts/image10.jpg'; }
// SATURDAY SCHEDULE
if ($d == 6 && $h >= 0 && $h < 1) { $img = 'images/hosts/image10.jpg'; }
else if ($d == 6 && $hhmm >= 0100 && $hhmm < 0542) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 6 && $hhmm >= 0542 && $hhmm < 0600) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 6 && $h >= 6 && $h < 7) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 6 && $h >= 7 && $h < 8) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 6 && $h >= 8 && $h < 9) { $img = 'images/hosts/image11.jpg'; }
else if ($d == 6 && $h >= 9 && $h < 10) { $img = 'images/hosts/image11.jpg'; }
else if ($d == 6 && $h >= 10 && $h < 11) { $img = 'images/hosts/image12.jpg'; }
else if ($d == 6 && $h >= 11 && $h < 12) { $img = 'images/hosts/image12.jpg'; }
else if ($d == 6 && $h >= 12 && $h < 13) { $img = 'images/hosts/image13.jpg'; }
else if ($d == 6 && $h >= 13 && $h < 14) { $img = 'images/hosts/image13.jpg'; }
else if ($d == 6 && $h >= 14 && $h < 15) { $img = 'images/hosts/image14.jpg'; }
else if ($d == 6 && $h >= 15 && $h < 16) { $img = 'images/hosts/image14.jpg'; }
else if ($d == 6 && $hhmm >= 1600 && $hhmm < 1730) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 6 && $hhmm >= 1730 && $hhmm < 1930) { $img = 'images/hosts/image15.jpg'; }
else if ($d == 6 && $hhmm >= 1930 && $hhmm < 2000) { $img = 'images/hosts/image8.jpg'; }
else if ($d == 6 && $h >= 20 && $h < 22) { $img = 'images/hosts/image13.jpg'; }
else if ($d == 6 && $h >= 22 && $h < 23) { $img = 'images/hosts/image10.jpg'; }
else if ($d == 6 && $h >= 23) { $img = 'images/hosts/image10.jpg'; }
// SUNDAY SCHEDULE
if ($d == 0 && $h >= 0 && $h < 1) { $img = 'images/hosts/image15.jpg'; }
else if ($d == 0 && $hhmm >= 100 && $hhmm <= 542) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 0 && $hhmm >= 0542 && $hhmm < 0600) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 0 && $hhmm >= 0600 && $hhmm < 0630) { $img = 'images/hosts/image15.jpg'; }
else if ($d == 0 && $hhmm >= 0630 && $hhmm < 0800) { $img = 'images/hosts/image3.jpg'; }
else if ($d == 0 && $h >= 8 && $h < 9) { $img = 'images/hosts/image11.jpg'; }
else if ($d == 0 && $h >= 9 && $h < 10) { $img = 'images/hosts/image11.jpg'; }
else if ($d == 0 && $h >= 10 && $h < 11) { $img = 'images/hosts/image12.jpg'; }
else if ($d == 0 && $h >= 11 && $h < 12) { $img = 'images/hosts/image12.jpg';  }
else if ($d == 0 && $h >= 12 && $h < 13) { $img = 'images/hosts/image13.jpg'; }
else if ($d == 0 && $h >= 13 && $h < 14) { $img = 'images/hosts/image13.jpg'; }
else if ($d == 0 && $h >= 14 && $h < 15) { $img = 'images/hosts/image14.jpg'; }
else if ($d == 0 && $h >= 15 && $h < 16) { $img = 'images/hosts/image14.jpg'; }
else if ($d == 0 && $hhmm >= 1600 && $hhmm < 1730) { $img = 'images/hosts/host2.jpg'; }
else if ($d == 0 && $hhmm >= 1730 && $hhmm < 1930) { $img = 'images/hosts/image15.jpg'; }
else if ($d == 0 && $hhmm >= 1930 && $hhmm < 2000) { $img = 'images/hosts/image8.jpg'; }
else if ($d == 0 && $h >= 20 && $h < 22) { $img = 'images/hosts/image13.jpg'; }
else if ($d == 0 && $h >= 22 && $h < 23) { $img = 'images/hosts/image10.jpg'; }
else if ($d == 0 && $h >= 23) { $img = 'images/hosts/image10.jpg'; }
?>
<img src="<?php echo $img; ?>">

 

Viewing all articles
Browse latest Browse all 2

Trending Articles