Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jeremy Frimond
Jeremy Frimond
14,470 Points

Date Time function in PHP to recognize users local time

I have made an ifelse statement to greet a user to website based on the time of day. The code works however it only recognizes GMT-0

Is there a parameter i can pass or function I can use so the statement will recognize the time zone of that person accessing the website?

I have attached my code below as reference:

<?php  
    $hour = date( 'G' );
    if($hour < 12){
        echo "<h1>Good Morning</h1>";
    }
    elseif ($hour >= 12 and $hour < 17) {
        echo "<h1>Good Afternoon</h1>";
    }
    elseif ($hour >= 17) {
        echo "<h1>Good Evening</h1>";
    }


?>

1 Answer

Jeremy Frimond
Jeremy Frimond
14,470 Points

Logan R thanks, I have been looking over and I dont know what parameter to pass in order for it to look for where the user is rather than me setting the default timezone? Any ideas?

Cheers

Logan R
Logan R
22,989 Points

I would look to see if you could do it with Javascript.

http://pellepim.bitbucket.org/jstz/

^ No guarantees that works, lol. Sorry.

Jeremy Frimond
Jeremy Frimond
14,470 Points

thanks for the quick help. I will look into it!