Note that there are some explanatory texts on larger screens.

plurals
  1. POAutomatically detect user's current local time with JavaScript or PHP
    primarykey
    data
    text
    <p>I often need to display information based on or influenced by a user's <em>actual</em> local time which differs across time zones. Is there a reliable way of getting a user's current time and/or timezone?</p> <p>Key Issues:</p> <ul> <li>Server-side code is based on the website host or user's ISP</li> <li>Client-side code is based on the user's system clock which is too easily manipulated</li> </ul> <p>Key questions:</p> <ul> <li>Is it necessary to pinpoint a user's geo-location?</li> <li>Can the ISP time be an accurate guide?</li> <li>Users OS clocks can vary or be modified by users?</li> </ul> <p>Examples:</p> <p><strong>Countdown:</strong></p> <blockquote> <p>Only 1 hour, 3 minutes, 56 seconds to go!</p> </blockquote> <p>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)</p> <p><strong>Edit log</strong></p> <blockquote> <p>Item posted at 3:03pm (10 seconds ago)</p> </blockquote> <p>Issue: "10 seconds" is calculated regardless of user, the time is adjusted to match the user's time zone.</p> <p><strong>Opening times:</strong></p> <blockquote> <p>This restaurant is open now until midnight (AEST).</p> </blockquote> <p>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.</p> <p><strong>PHP:</strong> using <code>time()</code> outputs the server time. This can be offset depending on the user's timezone.</p> <p><strong>JavaScript:</strong> using <code>Date()</code> outputs the user's computer clock time which is too easily manipulated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload