Page 1 of 1
time base greeting all timezones
Posted: Sat Apr 11, 2009 4:56 pm
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";
}
?>
Re: time base greeting all timezones
Posted: Sat Apr 11, 2009 5:12 pm
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)
Re: time base greeting all timezones
Posted: Sat Apr 11, 2009 5:26 pm
by gwolff2005
Hi,
Thanks for the hint. I will try first thing tomorrow morning!
Re: time base greeting all timezones
Posted: Mon Apr 13, 2009 10:51 am
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
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!