To create a time-based greeting in PHP, you can use the date()
function to get the current time and then display a different greeting based on the time of day. Below is an example of how to achieve this:
PHP Function
<?php
$vDate = new DateTime('now', new DateTimeZone('Europe/Amsterdam'));
$date = $vDate->format('H');
if ($date >= 6 && $date < 12) {
$greeting = "Good Morning";
$image = "morning.jpg";
}
else if ($date >= 12 && $date < 18) {
$greeting = "Good Afternoon";
$image = "afternoon.jpg";
}
else if ($date >= 18 && $date < 22) {
$greeting = "Good Evening";
$image = "evening.jpg";
}
else if ($date >= 22 && $date < 4) {
$greeting = "Good Night";
$image = "night.jpg";
}
else {
$greeting = "Hello";
$image = "art.jpg";
}
?>
HTML and PHP
<div class="demo-container" style='background-image:url("images/<?php echo $image;?>")'>
<h1><?php echo $greeting;?></h1>
</div>
Arithmetic operators
Background that adapts to the current season
Calculate the similarity of text
Comparison operators
Count page views in a session
Display a random image
Display a random text
Display array values
Display a time based greeting
Display the current copyright year
Display current date
Display the current date in another language
Display date based on system language settings
Quiz with input fields
Quiz with input fields, multi-steps
Quiz with radio buttons
Quiz with radio buttons, multi-steps
Redirect users to another page
Retrieve checkbox values
Retrieve form input values
Retrieve the value of the selected option
Retrieve the value of the star rating
Store the value of a radio button in a session
Truncate a string to a specified width
Word Wrap