PHP

Display a time based greeting

Hello

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>