Automatically detect user's current local time with JavaScript or PHP

I often need to display information based on or influenced by a user's actual local time which differs across time zones. Is there a reliable way of getting a user's current time and/or timezone?

Key Issues:

  • Server-side code is based on the website host or user's ISP
  • Client-side code is based on the user's system clock which is too easily manipulated

Key questions:

  • Is it necessary to pinpoint a user's geo-location?
  • Can the ISP time be an accurate guide?
  • Users OS clocks can vary or be modified by users?

Examples:

Countdown:

Only 1 hour, 3 minutes, 56 seconds to go!

Issue: seconds to go based on what; The user's OS clock, the hosts server time (plus or minus offset), ISP system time. This is easy if the clock finishes at the same time for all timezones, but trickier if it's a countdown to something like midnight in the user's local time zone)

Edit log

Item posted at 3:03pm (10 seconds ago)

Issue: "10 seconds" is calculated regardless of user, the time is adjusted to match the user's time zone.

Opening times:

This restaurant is open now until midnight (AEST).

Issue: Giving a time such as midnight can be calculated by offsetting from the server-side but using psudo times such as now, in 2 hours is based on the user's time.

PHP: using time() outputs the server time. This can be offset depending on the user's timezone.

JavaScript: using Date() outputs the user's computer clock time which is too easily manipulated.

Answers:

Answer

In short no.

I would suggest using server side time, and have the ability for the user to choose their time zone.

You could possibly calculate default time zones for users based on heuristics around their IP address, but this is open to error.

You should be able to extract client side time zone information through javascript / Ajax, but as with the time itself this is also open to error.

My recommendation: Let users choose their time zone with a sensible default based on where you expect your users to be.

Answer

With JavaScript, there is a way to get the users current timezone offset.

new Date().getTimezoneOffset() * -1

returns the offset in minutes from GMT. Do not store this value as it can change depending on Daylight Saving Time. If you need it on the server (php) then you'll need to capture it in send it.

Unfortunately, the HTTP spec doesn't send the users timezone as a header, which is what php would need.

Answer

I would suggest you write the server date out to javascript. Then do a new date in javascript to get date set by user on their machine. Use the different in the two dates.

With the server date you are sure that your time is unbiased. All calculations must be done using server date.

User the difference only for display purposes. This would work well for edit logs, countdowns and selecting messages like "Good morning".

If a user wants his time to be something else... I think you should let him have that.

Answer

PHP has both a PECL extension and a PEAR package that can do GeoIP lookups. Assuming the user isn't behind a proxy or NAT of some kind, the database is up-to-date and accurate, and the address is in the database at all, you could get the timezone, and calculate the offset from the server time.

Or, you could just accept that the user's system clock is not 100% accurate, but it's usually very close. All major operating systems have some way of syncing their clocks with network time servers, most of these are turned on be default. If the person has decided to put their computer in the "wrong" time zone, it's probably for a reason (they're traveling, they work with people in another time zone).

If the user has for some reason set their clock to be 10 minutes off, they accept the consequences of time calculations being inaccurate.


Added: If your server clock is accurate (if not, like HermanD said, fix it if you control the server, complain if you don't) but is not in the right timezone, you can always use PHP ini_set() or date_default_timezone_set() to force your scripts to operate in GMT, regardless of system time zone.

Answer

javascript:

<script type="text/javascript">
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()

if (minutes < 10){
  minutes = "0" + minutes;
}
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}

document.write("<b>" + hours + ":" + minutes + " " + suffix + "</b>");
</script>

output

11:51 AM

Answer

Id recommend using server time. In example, the time() function. If you base EVERYTHING off this value, then your system will be safe from any time specific operations you need.

Everytime a user requests a page that has or uses time contraints, make sure you send the server time.

Create a Javascript module that, on page load, checks if the server has sent the time, and if so reads the value, and converts it to the JS local time, using a JS fetched or calculated time zone. Then you could save it as a JS global or slam it as a static var in an object or something.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.