Log in

View Full Version : How to pass browser/javascript based local time to php variable (ajax?)



kenax
05-08-2013, 04:36 AM
hi, i've spent days trying to figure this out and have a similar problem. I've learned html, css, php and others, but ajax baffles me somewhat. Starting to get a hang of it, but all the tutorials I've come across are typically convoluted and not so clear. I'm working on
http://001yourtranslationservice.com/kenax/Translators/Resources/TimeZones.htm
and trying to change the "Your time" at the top (of the alarm clock) based on the user, which requires javascript. I assume the procedure is for js to first determine their local time (probably easiest just from their computer time settings, although I've found some pretty fancy scripts based on internet connection), then to save this little bit of information to an xml file via ajax, then for the js to launch the php script, which will draw the data from the xml file and do its wonders (right now it's php only and the time at the top is based on the server's location, somewhere in the US). The js would be set to refresh the page once a minute. For now the php script is doing that by html page refresh within the iframe. There must be a simpler way to do this then to use jQuery. i'd like to know the basics and not create a monster to do this simple task. Thank you very kindly for your help!

jscheuer1
05-08-2013, 06:14 AM
I'm a little unclear here as to what's going on/being asked. Is that your page you've linked to?

In any case, an XML file shouldn't be required. Javascript/AJAX can pass the user's time and/or timezone offset directly to a PHP page. In fact, AJAX might not be required. Any PHP page which loads into the browser can use javascript to get the user's time.

It might actually be better to use PHP (or any available server side code) to get the server time and use that to find the target time in the client's timezone, then use javascript to calculate the user's time and do the other work to setup the alarm clock.

For an example of a script that does something like that, see:

http://www.dynamicdrive.com/dynamicindex6/universalcountdown.htm

But if you still want to write an XML file with the user's local time in it, AJAX alone cannot do that. But it can send the data to the server, which can use PHP to write the XML file. But I think the user's minutes difference from UTC might also be helpful, so:

timedemo.htm:


<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript">
(function($){
var date = new Date();
$.ajax({
url: 'makexmlfile.php',
data: {'zoneoffset' : date.getTimezoneOffset(), 'usertime': Math.round(date.getTime() / 1000)},
cache: false/* ,
success: function(data){ //uncomment this red section for diagnostics if desired
console.log(data);
} */
});
})(jQuery);
</script>
</head>
<body>

</body>
</html>


makexmlfile.php:


<?php
if(!isset($_REQUEST['zoneoffset']) or !isset($_REQUEST['usertime'])){die();}
$useroffset = $_REQUEST['zoneoffset'];
$usertime = $_REQUEST['usertime'];
$xml = "<data>
<offset>$useroffset</offset>
<usertime>" . date('Y-m-d-H-i-s', $usertime) . "</usertime>
</data>";
file_put_contents('user.xml', $xml); //requires PHP 5 or greater, there is a fall back if needed for PHP 4
?>

A typical resulting user.xml file:


<data>
<offset>240</offset>
<usertime>2013-05-08-02-06-52</usertime>
</data>

But this is inferior to any real time processing (like the first idea in this post, and others that could be worked out) due to the likelihood that two users might collide over the use of the XML file, unless a unique XML file is generated for each user.