2

is possible to know the local time and date of the user who opens a php page? (not server side time and date). If the answer is no, can show me an example of javascript combined with php? (im not a java developer) Thanks.

DomingoSL
  • 14,920
  • 24
  • 99
  • 173
  • Perhaps not relevant, but an alternative that's often used is to let the user specify their time zone in their user preferences. Then again, the site in question might not have such a thing. – John Parker Dec 03 '10 at 16:41
  • 1
    What exactly do you need it for? There are a few ways to do it, but if you want a good answer you'll have to tell us why :) – Adrian Mester Dec 03 '10 at 16:47
  • 1
    No, for good answer we demand a candy in advance ;) – Free Consulting Dec 03 '10 at 17:50

2 Answers2

4

First, Java is not JavaScript…

And you could do this through hidden <input> in HTML.

<input id="user-time" name="user-time" type="hidden" />

and JavaScript function to associate with form on submit:

function submit() {
    document.getElementById("user-time").value = (new Date()).getTime();
}

After this, parse the timestamp in your server-side PHP.

Ryan Li
  • 9,020
  • 7
  • 33
  • 62
1

Yes but it wouldn't be a 100% accurate. Using their IP you can determine what timezone they are in. See this question about doing just that.

Once you have their timezone you can use date_timezone_set to set the appropriate timezone in PHP and pull their date/time information.

Community
  • 1
  • 1
John Strickler
  • 25,151
  • 4
  • 52
  • 68
  • Knowing the timezone of the IP address is not the same thing as knowing the timezone of the *computer* that the browser is actually running on. If somebody hits a site via a VPN proxy, for example, the IP address could be of someplace thousands of miles away from the actual person looking at the site. – Pointy Dec 03 '10 at 16:41
  • @Pointy, well, yeah. But al least this method works for the vast majority, without the user needing to submit a form first. – Horia Dragomir Dec 03 '10 at 16:49