Forum Moderators: coopster
In any case, even if everyone set her computer to the exact time, with the number of folks travelling around with laptops, you can never have reasonable certainty that the client computer time corresponds to the user's time.
For instance, you can keep a session going with the user's clock offset in it (you can figure out the difference between server time and client time, and store that as +/- minutes). If the session has a value, you use it. If it is empty, you force the current page to reload with something like this:
<script language='javascript'>
// get the clock time offset
var timezone=<?php print(mktime())?>-getDate();
// or whatever works, I'll leave that up to you
// reload the page with that in the querystring
location.href='<?php print($PHP_SELF)?>?timezone='+timezone
</script>
When the page reloads, you'll have the client's information sitting there in the querystring. How convenient!
Test it, and make sure it will "fail gracefully" (you don't want infinite reloading if the sessions don't stick or if javascript is disabled)
good luck
PHP is a server-side script - that should answer your 1st question
I had a feeling the answer might be someething like that ;)
I will go with js. I'm only using this script for a bit of fun so it doesn't really matter if people have their clocks set wrong. In fact maybe I can have some extra fun with that too...
Cheers,
H.
1. Download free geoip database. Insert into your DB.
2. Write a simple script to compare visitor's IP to the geoip DB. This will provide you with the country they are from.
3. Work out the time difference between your server and the visitor's country. I'm just doing this for the UK and USA at the moment, ignoring other countries.
4. Update any variables accordingly. (On my CMS I set a global $timezone (E.g. '+ 5 hours') which is set using the geoip function at the start of each page. Then in any SQL Select statements I can do 'SELECT time $timezone'.)
I'm using this system on various sites powered by my custom CMS and it works pretty well. Visitor's don't need to set their time zone, all times throughout the site are correct for them and they can be shown geographically relevant adverts.
The only drawback is the free geoip databases have something like 98% accuracy, but I can live with that. :)
I remember in 1999 all the sites with Javascript Y2K countdown clocks ("you have xx hours, xx minutes, xx seconds to become Y2K compatible"). (One organization's site that I ran had the same theme but based on Y3K and "We are ahead of the rest!)
Hmm..determining whether a client's clock is set correctly may be a great use for something like this.
You could have some fun with that. Just make sure that your server's clock is accurate. I have seen some that were a minute or more off.
<?
session_start();
if($_GET["time"]){
$_SESSION["SentTime"] = $_GET["time"];
}
?>
<html>
<head>
<title>Get Time</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<img src="" name="timeimage" width="1" height="1" id="timeimage">
<SCRIPT LANGUAGE="JavaScript"><!--
var now = new Date();
document['timeimage'].src=''+'?time='+now;
//--></SCRIPT>
<?
echo $_SESSION["SentTime"];
?>
</body>
</html>
Note that this session value won't be available on the FIRST request, as the server didn't have any way of knowing you were looking for the time on that request. The javascript call to the pixel is actually the second request. All subsquent requests will pass the time back to the browser. This requires the use of a session.
Just something to play with.
Bill
Does your Geo solution take Daylight Saving into account and does it know which states and provinces change their clocks at different times? (like the special case of Indiana - 77 of the state's 92 counties are in the Eastern Time Zone but do not change to daylight time in April. Instead they remain on standard time all year. That is, except for two counties near Cincinnati, Ohio, and Louisville, Ky., which do use daylight time.
bstrz, that's a very cool trick; I've done that pixel request thing in other applications, but it didn't occur to me to use it to solve this problem. kudos!