Forum Moderators: coopster

Message Too Old, No Replies

using php to read clock

can php read the time on the client's machine?

         

HelenDev

12:53 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I want to display a different image depending on the time of day, can I use php for this? Can php read the time on the user's machine or only the server time? Would I be better off using javascript for this?

If anyone can point me in the right direction I would be grateful.

cheers,
Helen.

lorax

3:02 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



PHP is a server-side script - that should answer your 1st question. ;)

Javascript would be better but given the lack of computer knowledge out there - and the number of computer clocks I've personally seen that have been as much as several years off - do you really want to trust them?

ergophobe

3:11 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To follow up on lorax, for users with login accounts, you could ask them to set the time zone (though of course, I've never bothered to do this on WebmasterWorld) and have the form default to the client time using JS.

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.

httpwebwitch

3:51 pm on Jul 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Browser variables can't be grabbed immediately, but with very little effort you can command the browser to send them to you. Use javascript to reload the current page with some client variables appended to the querystring.

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

HelenDev

10:45 am on Jul 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cheers for all your replies, guys :)

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.

timster

12:39 pm on Jul 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and the number of computer clocks I've personally seen that have been as much as several years off - do you really want to trust them?

Hmm..determining whether a client's clock is set correctly may be a great use for something like this.

Wi11

1:21 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



PHP is a Server-side scripting language but it captures client data, so...

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. :)

ControlEngineer

1:41 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



Javascript can be used when using the client machines clock, PHP would use the server clock.

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.

bsterz

2:39 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



Kinda fun way of doing it:

<?
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

httpwebwitch

2:42 pm on Jul 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We11,
Your GeoIP method seems awkward to me; Why would you infer someone's clock settings from a 10% inaccurate geographical deduction based on an IP address... then introduce more inaccuracy by using a time zone assumptions that may not always be correct? The system clock is ticking there, just waiting for you to grab its value.

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!