Author Topic: How to set custom greeting message based on time of the day  (Read 496 times)

christogonus

  • Newbie
  • *
  • Posts: 6
    • View Profile
How to set custom greeting message based on time of the day
« on: April 16, 2019, 01:51:10 PM »
When trying to make a greeting message for your website visitors, there are 2 main questions that comes to mind.
  • What timezone is your client on?
  • What timezone do you want the greeting in?

Wondered why you get "GOOD MORNING" When you visit a website in the afternoon? It is simple, the time is being taken probably from the server time or the programmer used a timezone that is different from yours. So, I guess answering these questions solves 50% of the challenge.

Now, I always prefer you pick the visitor's time and timezone from the visiting device - remember it is a customized message  :)

Alright, into the main deal...

Quote
My example will be done with PHP, but it is pretty the same flow for every programming language.

First, I will have the client's timezone at the top of the page(s)

Code: [Select]
$timzoneInfo = file_get_contents('http://ip-api.com/json/');
$timezoneInfo = json_decode($timzoneInfo);
$timezone = $ipInfo->timezone;
date_default_timezone_set($timezone);

Then, I will write a function to return the greeting as follows

Code: [Select]
function GetGreeting($clientTime = date("G")) {
  //Get user timezone
    if ($clientTime < 12) {
      $greeting = 'good morning';
    } else if ($clientTime < 17) {
      $greeting = 'good afternoon';
    } else {
      $greeting = 'good evening';
    }
    return $greeting;
}


But if what you want is to show time like "5 days ago", then timeago jquery plugin is a good option to make use of.