I’m new to PHP and I have this homework to do. The teacher asked us to display a greeting message to the user which should look like:
Good(Morning/Evening/Night), username, actual date(taken from the system).
PHP:
$username = "Foo";
if (date("H") > 0 && date("H") < 12) {
$msg = "Good day";
$image = "r_sun";
}
else if (date("H") >= 12 && date("H") < 18) {
$msg = "Good evening";
$image = "sun";
}
else {
$msg = "Good Night";
$image = "moon";
}
if (date("d") == 1) {
$c = "st";
}
else if (date("d") == 2) {
$c = "nd";
}
else if (date("d") == 3) {
$c = "rd";
}
else {
$c = "th";
}
echo $msg . "<img src='images/" . $image . ".png' border='0' />, " . $username . "! Today is " . date("F") . " " . date("d") . $c . ", " . date("Y");
My problem is with the image. I have to show 3 different images depending on which message is being displayed (Good morning, evening or night). For some reason the image won’t load on the page.
You have to add
../before theimagespath on yoursrcattribute like this:This means that your
imagepath is not on the same directory as your script, like you doing.Plus, just a tip, place the
datevalues on an variable:Will clear your code 😉