3

I am working on a project which primarily uses javascript, css, html5. I need to get the local time accurately no mater where the user is located to allow user to access a module on a particular date. Assume 1 September.

How do i get the users local time accurately?

Options:

1: Use JavaScript to get the users local time and use it.

problem: The user can manually change the date time settings of his system to change the date and access the module prematurely.

2: Use server date time to enable a module on a particular date.

problem: The server could be located anywhere eg: in U.S. and people in Australia will not be able to access the module unless the date in U.S is 1 September.

Is there any other option.

Is using client IP address a option?

Shrujan Shetty
  • 2,298
  • 3
  • 27
  • 44
  • 1
    You cannot rely on JS date, that's user controlled. You also cannot rely on IP because the request might be coming through a proxy. There really is no answer to this question other than data at your disposal is unreliable if you can't trust your users. – Sergiu Paraschiv Aug 19 '14 at 09:30
  • look at here : http://stackoverflow.com/questions/1531093/how-to-get-current-date-in-javascript – may saghira Aug 19 '14 at 09:33
  • You can't trust the user's time or anything else they report (they can lie). And you can't trust their IP to reflect their location (could be a VPN, and this might be private and so not identified as such in any IP lookup. Though that in itself is a question for you: if an employee of a US company works from home in Australia, what timezone is appropriate for your purposes?). And how about summer time (etc) options - do you really need to care about local time? For a truly robust, consistent, and uncontroversial approach you probably need a flat earth (and I'm only half-joking...) – Tom Goodfellow Aug 19 '14 at 09:35
  • I think you could still use server time and use data from their local data (using javascript) to get only the timezone. If you still cannot trust your users, you may ask them to confirm the timezone, or choose it themselves from the very start. – Lukas1 Aug 19 '14 at 09:36
  • IP is as reliable as the system time. The client could always use a proxy to connect to your site. – pstenstrm Aug 19 '14 at 09:36
  • Thanks guys for your reply.. seems that server side date is more reliable than client side... Thanks again... – Shrujan Shetty Aug 19 '14 at 09:49
  • You can probably rely on the user's timezone (which you can get using javascript). With it, you can convert a date/time from the server (GMT) to the user's local time. – Sebastien C. Aug 19 '14 at 10:34

3 Answers3

0

First of all you should always assume user may fake any data calculated on his side. Therfore using server time is more reliable.

Using IP is an option - you can find services and databases that allow you to resolve IP to country its located in. Example: http://php.net/manual/en/book.geoip.php

Lastly - why do you want efectively differend release date for various countries? They can always use someone in other country to access module in their name.

DzikiMarian
  • 423
  • 3
  • 14
  • Knowing the country isn't enough to get the local time accurately as there can be multiple time zones in a country. – Nick Rice Aug 19 '14 at 09:53
  • Yes, however most of databases will return state/region also. There is even PHP extension mentioned in PHP docs that does that(added to oryginal reply). – DzikiMarian Aug 19 '14 at 11:09
  • OK, if IP address can be resolved to state/region then I stand corrected. – Nick Rice Aug 19 '14 at 12:23
0

well, the user's time/date info is not included in the http request header, so php will not automatically have that information. You can, as you said, use javascript to get the user's time similar to what was posted here: Determine a User's Timezone -- this is with pure javascript, if you use jquery or something similar to it, you can do it very easily.

if these are registered users however, you can allow them to set a timezone in their profile/settings, and then just use THAT setting, so even if they are traveling, they will always be set to the "home" timezone.

does that help?

Community
  • 1
  • 1
Garry
  • 9
  • 1
0

If the user gives permission, and is using a supported browser, etc, you can get their location using navigator.geolocation.getCurrentPosition().

See developer.mozilla.org/en-US/docs/Web/API/Geolocation.getCurrentPosition for parameters and more info.

You can then use a service such as provided by geonames.org. Eg, http://api.geonames.org/timezone?lat=47.01&lng=10.2&username=demo . This returns the time at the given coords.

Update as per first comment: Of course you can never trust any data coming in from outside. But you can do things to raise levels of confidence. This wasn't meant to be a full stand-alone solution.

Nick Rice
  • 1,163
  • 1
  • 13
  • 21
  • It does not matter how you obtain the timezone, in the end you are still relying on data sent by the user. And anyone can send anything. – Sergiu Paraschiv Aug 19 '14 at 12:04
  • Yes of course. That's been covered elsewhere here and is one of the first things everyone learns. It was just a basic answer to add further options. There is no single correct answer for this question, in my opinion, and it needs a combination of things to increase the level of confidence. – Nick Rice Aug 19 '14 at 12:09
  • I understand that, but I still think this is not an answer. If a user decides he will spoof his location then he will be able to change what he sends _and_ his IP. There's no answer to this question other than "not possible, find a way not to care if users spoof their location". – Sergiu Paraschiv Aug 19 '14 at 12:14