time base greeting all timezones

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

time base greeting all timezones

Post by gwolff2005 »

Hi guys,
I need your help. I have a script which shows the time on my server, but does not realize the clients timezones. How can I greet my customers according to THEIR timezone?

Code: Select all

print date("g:ia"); 
 
$date = date ("H"); 
 
if ($date < 6) { 
echo "Good Morning "; 
} elseif ($date < 7) { 
echo "Good Morning"; 
} elseif ($date < 12) { 
echo "Good Morning "; 
} elseif ($date < 18) { 
echo "Good Afternoon "; 
} elseif ($date < 22) { 
echo "Good Evening "; 
} elseif ($date < 24) { 
echo "Good Evening"; 
} 
 
?>
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: time base greeting all timezones

Post by Mark Baker »

You either need to keep a copy of the user's timezone in their profile (assuming that users register on your site), or you need to use a client-side technology such as javascript to read their timezone and pass it through to the server. In the latter case, you might find it easier to use javascript to display the greeting rather than PHP.

However, your PHP code can be simplified:

Code: Select all

 
$hour = date ("G"); 
 
if ($hour < 12) { 
   echo "Good Morning "; 
} elseif ($hour < 18) { 
   echo "Good Afternoon "; 
} else { 
   echo "Good Evening "; 
} 
 
Which also fixes the bug with 24 (there is no hour 24)
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: time base greeting all timezones

Post by gwolff2005 »

Hi,
Thanks for the hint. I will try first thing tomorrow morning!
gwolff2005
Forum Commoner
Posts: 68
Joined: Sun Apr 05, 2009 10:58 am

Re: time base greeting all timezones

Post by gwolff2005 »

Hi. I need your help!
I have a code, which should realize the time on the clients computer.

Code: Select all

<script type="text/javascript">/*<![CDATA[*/
        function clock()
        {
            
            //generate time
            var time = new Date();
            var hours = time.getHours();
            var minutes = time.getMinutes();
            var seconds = time.getSeconds();
            
            //add preceding 0s, if required
            var hours = (hours < 10 ? '0' : '')+hours;
            var minutes = (minutes < 10 ? '0' : '')+minutes;
            var seconds = (seconds < 10 ? '0' : '')+seconds;
            
            //generate formated time
            var time = hours+':'+minutes+':'+seconds;
            
            //display time
            document.getElementById('clock').innerHTML = time;
        }
        //init clock
        window.onload = function()
        {
            clock();
            setInterval('clock()', 1000);
        }
    /*]]>*/
</script>


and I entered

Code: Select all

<span id="clock"></span>
what do I have to write now to make it visible for the users to see either good morning/afternoon/evening depending on THEIR time?

Thanks so much!
Post Reply