Hi I have this PHP code which I expect should be working however it is currently showing a blank page.
How can I get this working on my HTML site? As when I look on the php file on the local host it doesnt come up with anything just blank. What I want this for is so that different times of the day a different images appear, this is for a radio show. But I cant seem to get it working properly.
<?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 >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 1 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h < 0) $img = 'img/hosts/test2.jpg';
// TUESDAY SCHEDULE
if ($d == 2 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 2 && $h < 0) $img = 'img/hosts/test2.jpg';
// WEDNESDAY SCHEDULE
if ($d == 3 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 3 && $h < 0) $img = 'img/hosts/test2.jpg';
// THURSDAY SCHEDULE
if ($d == 4 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 4 && $h < 0) $img = 'img/hosts/test2.jpg';
// FRIDAY SCHEDULE
if ($d == 5 && $h >= 12 && $h < 14) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 14 && $h < 16) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >=16 && $h < 18) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 18 && $h < 20) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 20 && $h < 22) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 22 && $h < 24) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h >= 19) $img = 'img/hosts/test2.jpg';
else if ($d == 5 && $h < 0) $img = 'img/hosts/test2.jpg';
CodePudding user response:
- you do not have a default image
- you do not echo the image to the page at the end of the page
- your schedules are the same for each day and your are setting the same image for every time
I would expect this:
http://sandbox.onlinephpfunctions.com/code/556ecb6463298fcfa82f93372a000b9738d911d9
<?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;
$img="img/hosts/off_air.jpg"; // default
if ($h >= 12 && $h < 14) $img = "img/hosts/test$d_12to14.jpg";
else if ($h >= 14 && $h < 16) $img = "img/hosts/test$d_144to16.jpg";
else if ($h >=16 && $h < 18) $img = "img/hosts/test$d_16to18.jpg";
else if ($h >= 18 && $h < 20) $img = "img/hosts/test$d_18to20.jpg";
else if ($h >= 20 && $h < 22) $img = "img/hosts/test$d_20to22.jpg";
else if ($h >= 22 && $h < 24) $img = "img/hosts/test$d_22to24.jpg";
//else if ($h >= 19) $img = 'img/hosts/test2.jpg'; // already handled
else if ($h < 12) $img = 'img/hosts/test$d_morning.jpg';
echo "<img src=\"$img\" />";
?>
Now you can have images like
test1_12to14.jpg
for Monday from 12 to 14
and
test3_22to24.jpg
for Wednesday from 22 to 24
CodePudding user response:
you have not declare the scope or condition for hour less than 12 your first condition should be
if ($d == day_number && $h < 12) $img = 'img/hosts/test2.jpg';