How do I display the visitor's local time?

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
wwmmww
Forum Newbie
Posts: 4
Joined: Tue Apr 29, 2008 6:37 pm

How do I display the visitor's local time?

Post by wwmmww »

Right now I have this code on my site

Code: Select all

 
<?php
echo date("l \\t\h\e jS \o" . "f" . " F, g:i a");
?>
 
But it only shows the server time and not the visitor's

How can I do this?


Thanks!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How do I display the visitor's local time?

Post by onion2k »

It can't be done with PHP. There's nothing communicated from the user's computer to the server to tell you their time or timezone. You can do it with Javascript, but that's a bit of a hassle and it won't work if the user has Javascript switched off.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: How do I display the visitor's local time?

Post by Eran »

You will have to know the user's timezone to do this. If you have the timezone you can set it using date_default_timezone_set() - http://www.php.net/manual/en/function.d ... ne-set.php, and the the date function will format it accordingly.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: How do I display the visitor's local time?

Post by jmut »

pytrin wrote:You will have to know the user's timezone to do this. If you have the timezone you can set it using date_default_timezone_set() - http://www.php.net/manual/en/function.d ... ne-set.php, and the the date function will format it accordingly.
@wwmmww In that regard.... it's usual practice to let customer set their timezone on registering and/or configure it whenever they want.... cause other means are just unreliable.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: How do I display the visitor's local time?

Post by Eran »

thats what i meant.. they have to set it in their profile / session whatever and in the php you enforce it using the function i mentioned. You don't expect that just setting it in the profile will have any effect?
Post Reply