Good morning/good evening PHP function

How can I get php coding of good morning/good evening script when a user enter the site then it is show greeting.

A very simple solution, but a solution nonetheless. Take into account the different time zones of people around the world though. You might be looking for a more advanced solution if you want to change the timezone for each user, but this will give you an idea.


function greeting(){

    $timeOfDay = date('a');
    if($timeOfDay == 'am'){
        return 'Good morning, welcome to our site';
        return 'Good afternoon, welcome to our site';


echo $timeOfDay;


Like bit10101 says, do you want to say “Good morning” depending on where the server is or where the user is?

This tut might help you grapple with some of the issues:

I’m always a sucker for GeoIP:

GeoIP won’t be as accurate as javascript which uses the user’s system clock and seems like overkill for this!

I don’t feel that’s overkill at all. Javascript can be just as inaccurate as GeoIP. Your counting on their clock to be set correctly, as well as the fact that Javascript can be turned off. All depends on its application and the necessity of the data.

On my shared server, geoIP functions don’t work, presumably they need activating somewhere, but I don’t have that kind of access. So, I would be restricted to javascript, in which case this might be helpful:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">

function getTime() {
    var dd = new Date();
    var hh = dd.getHours();

    if(hh > 11){
    document.write("Good afternoon");
    document.write("Good morning");

  <p><button onclick ="getTime()">Click for greeting based on time of local machine</button></p>

GeoIP is a PECL extension, so yes it would need installed on the server running it.