Please help fix this code

Can someone tell me how can i run this php code in html? I am trying to display an image based on time of the day and week of the day.

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

What problem are you having? Is it just that the code doesn’t run when you upload it to your server? If that’s the case, are you sure the server is correctly configured, and that you have the file named as something.php to make the server execute the code?

If it’s something else, can you expand on what it’s not doing?

1 Like

I just don’t know how to see the result of this code. Like how can show the front en of this code? When I save it as mywebpage.php its not showing me anything.

you need to add

echo $img;

to the end of the code to show the output.

Can you provide an example? Thank you.

You are setting the variable $img in your code but you aren’t using it anywhere.
If you want to print out the results or contents of the $img use echo

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 $img; 
?>

Got you. Thank you so much for your help! How can I display the result on a html page? Any idea?

No problem, simply use the <img tag in your HTML output

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

For some reason it’s not working. I tried it but not diisplay on the webpage

I am just trying to create a webpage that will display an image on a specific day and time. For example let say today is sunday: at 7.30 to 8am an image will display and so on? Can you help me out?

Thats what your code does now.
Show me how you are trying to display it, code please

The image is not displaying.

I name my php file: test.php and the schedule.html

PHP runs on a server and so you can’t just view it in a browser.
You would need to set up either a localhost server (WAMPP) should do and its easy to set up.

Then you can run the php code.

So you mean if i open the html part it won’t work right? How can i make it work? Is there any other language you know that will do the same thing? I appreciate your help so much.

PHP will work fine for what you want if you are running this through a server.
You could look at a javascript function that would do something similar if you don’t want to run PHP

i also have a javascript that i did: but does not work either

<!DOCTYPE html>
<html>
<body>
	<script>
	    var date = new Date();
	    var day = date.getDay();
	    var hour = date.getHours();
	    var minutes = date.getMinutes();
	    var img;

	    if (day > 0 && day < 6) {
	        if (hour = 19) {
	            if (minutes > 1) {
	                img = "image1";
	            } else {
	                img = "image2";
	            }
	        } else if (hour > 8 && hour < 14) {
	            img = "image1";
	        } else if (hour = 2) {
	            if (minutes < 43) {
	                img = "image1";
	            }
	        } else {
	            img = "image2";
	        }
	    } else {
	        img = "image2";
	    }

	    (function() {
	        document.getElementById("image").src = img + ".jpg";
	    })()
	</script>
</body>
</html>

post it up in the javascript forum, you should get help there :slight_smile:

Thanks. You are the best.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.