• Display a time-based greeting

    This has been covered in-depth all over the web, for years, but my own little take on it is a simple PHP function which does the job the way I want it done!

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    function get_greeting() {
    // Get 24-hour format of current time without leading zeros
    $numeric_date=date("G");

    // Generate time-based greeting
    if($numeric_date>=0 && $numeric_date<=6)
    $greeting="You're up late";
    if($numeric_date>=7 && $numeric_date<=11)
    $greeting="Good morning";
    else if($numeric_date>=12 && $numeric_date<=17)
    $greeting="Good afternoon";
    else if($numeric_date>=18 && $numeric_date<=23)
    $greeting="Good evening";

    return $greeting;
    }

    You could then simply echo this out with:

    1
    echo getGreeting() . " " . $user_name;
    Share and Enjoy:
    • Digg
    • Sphinn
    • del.icio.us
    • Facebook
    • Mixx
    • Google Bookmarks

    1 responses to “Display a time-based greeting”


    • Brett Borden

      This post was very nicely written, and it also contains a lot of useful facts. I enjoyed your professional manner of writing this post. Thanks, you


     Leave a reply