Grow your CSS skills. Land your dream job.

Time Greeting

  • # June 10, 2010 at 7:57 am

    Hey there guys, I’m trying to show a time greeting on my site. The only problem is that I’d like it to display "Good Afternoon" from 12pm onwards, this doesn’t seem to be working. Site link (http://hollandalekane.co.uk/)

    Code used:

    Code:
    < ?php
    $b = time();
    $hour = date("g",$b);
    $m = date ("A", $b);

    if ($m == "AM") { if ($hour == 12)
    { echo "

    Good Evening!“; }
    elseif ($hour < 4)
    { echo "

    Good Evening!“; }
    elseif ($hour > 3)
    { echo “

    Good Morning!

    “; }
    }

    elseif ($m == “PM”)
    {
    if ($hour == 12)
    { echo “

    Good Afternoon!

    “; }
    elseif ($hour < 7)
    { echo "

    Good Afternoon!“; }
    elseif ($hour > 6)
    { echo “

    Good Evening!

    “; }
    }
    ?>

    # June 10, 2010 at 10:36 am

    I’ve just had a look at your site and it says ‘Good afternoon’ for me.

    I’m in Spain and it’s 16:35 here, so it seems to be working fine.

    # June 14, 2010 at 11:01 am

    Is there a way to make my page display this?:

    - 12:00 AM – 12:00 PM > (Good Morning)

    - 12:00 PM – 06:00PM > (Good Afternoon)

    - 06:00 PM – 12:00 AM > (Good Evening)

    eXo
    # June 15, 2010 at 6:11 pm

    This works for me:

    Code:

    <?php
        $cD 
    date(“G”);
        if(
    $cD <= ’12′){
            echo 
    ‘Good Morning’;
        }else if(
    $cD <= ’18′){
            echo 
    ‘Good Afternoon’;
        }else{
            echo 
    ‘Good Evening’;
        }
    ?>

    But like this it will get the current server time. so if the visiter is from another GMT area then it doesn’t work correctly for that visiter.

    # June 16, 2010 at 4:23 pm

    If you use php, you will get the server time not the person’s local time (as eXo above me said). You can use javascript to get the person’s local time (or atleast the time they have on their computer) and render the page from there. A quick google search can give you the function for that.

    -T

    # June 18, 2010 at 5:38 pm

    I agree with the two above posts, it should be generated with Javascript

    Javascript:

    Code:
    window.onload = function () {
    timegreeting();
    }

    function timegreeting() {
    var now = new Date();
    var greet;
    /* If before 11:00 */
    if (now.getHours() < 11) {
    greet = "

    Good morning!“;
    }
    /* If between 11:00 and 17:00 (5:00, all times are expressed in 24 hr time) */
    else if (now.getHours() < 17) {
    greet = "

    Whatever“;
    }
    /* If after 17:00 */
    else {
    greet = “

    Good evening!

    “;
    }
    document.getElementById(“greeting”).innerHTML = greet;
    }

    HTML:

    Code:

    I used that script a long time ago, but it seems to work well

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".