Jump to content

Php-based Greeting


driz

Recommended Posts

Hi I've coded this:

<?php $hour = idate("H"); 			if($hour >= 12  && $hour < 17) { echo "Good Afternoon"; }			else if($hour >= 17 && $hour < 21) { echo "Good Evening"; }			else if($hour >= 21) { echo "Good Night"; }			else { echo "Good Morning"; } ?>

But this will return a value based on the server time and NOT the user time right? How do I use the user's time instead, because the servers are in the US, but obviously the user could be anywhere in the world (most likely in the UK). Thanks

Link to comment
Share on other sites

I've used this javascript to display a greeting:

function showgreeting(){    datetoday = new Date();    thehour = datetoday.getHours();        if (thehour > 16 || thehour < 3) display = "Evening";    else if (thehour > 11) display = "Afternoon";    else display = "Morning";        var greeting = ("Good " + display + "!");        document.write(greeting);}

HTH

Link to comment
Share on other sites

You can't use the user's local time if you're running a server-side script.Forums and other pages ask users to specify their time zone in their profile after signing up, that's how they do it.

Link to comment
Share on other sites

Stick with Ingolme, about what he said about the time-zones. Unless the browser sends their current time and date to the server i don't think their will be an function to return time and date. So go with the time zones.

Link to comment
Share on other sites

At the moment I have the function and then I call it where I want it. What other ways could I do this?

<?php	function createGreeting() {			$hour = idate("H"); 			if($hour >= 12  && $hour < 17) { echo "Good Afternoon"; }			else if($hour >= 17 && $hour < 21) { echo "Good Evening"; }			else if($hour >= 21) { echo "Good Night"; }			else if($hour >= 1 && $hour <= 6) { echo "Good Morning (Your up early!)"; }			else { echo "Good Morning"; }	}?>

<p><?php createGreeting(); ?>, <strong>Cloud Strife</strong> ( <a href="" title="Preferences">Preferences</a> | <a href="" title="Log Out">Log Out</a> )</p>

Link to comment
Share on other sites

Stick with Ingolme, about what he said about the time-zones. Unless the browser sends their current time and date to the server i don't think their will be an function to return time and date. So go with the time zones.
Surely PHP of doing this, seems rather silly to have to resort to JavaScript when PHP should be able to know the users timezone and then output something based on that.
Link to comment
Share on other sites

Surely PHP of doing this, seems rather silly to have to resort to JavaScript when PHP should be able to know the users timezone and then output something based on that.
What you don't seem to understand is that the PHP is running on the server, it has no access whatsoever to the user's machine. It has the same possibility of reading the user's time as it does to save files on the user's computer.
Link to comment
Share on other sites

Surely PHP can detect where the user is coming from via their IP or language settings?
An IP can allow you to detect the country, but not the IP.Take, for example, the USA. There are four time zones in the USA. Two people with IPs that start with the same number might still be on different time zones.The language settings are yet more vague (even if PHP could read them, which it can't because that information is on the client's computer): If their language is set to English, they could be anywhere in the USA, Canada, United Kingdom, New Zealand, Australia, Ireland and possibly not even in an English speaking country.
Link to comment
Share on other sites

Any information that PHP has about the browser can be found in the $_SERVER array, feel free to print that out and go through it to see what's available. You can't guarantee that the $_SERVER array will be the same for all browsers though, so browsers send different information to the server. Some browsers might send the time zone, some won't.

Link to comment
Share on other sites

Thanks for all the replies really appreciate it. I'm shocked that PHP is unable to achieve my goal, will have to have a think about it.Coming back to my other question though about the function, is that a suitable way of doing it? What other ways are they (want to learn). Thanks

Link to comment
Share on other sites

I guess it's as good as any other. If you want PHP to get the user's time zone, you need to have Javascript send PHP a timestamp that you can compare to figure out the offset. If you're not interested in dates from a database or something, like the function you have here, you might as well do that entirely in Javascript. If you have a timestamp from a database that you want to display in the user's local time, have PHP send the timestamp to Javascript so that it can write the date string out. Or, if you have user accounts, like people mentioned the easiest way is often to just ask the user which time zone they're in.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...