Results 1 to 2 of 2

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

  1. #1
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to pass browser/javascript based local time to php variable (ajax?)

    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.../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!

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,138
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    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/dynamici...lcountdown.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:

    Code:
    <!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 Code:
    <?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:

    Code:
    <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.
    Last edited by jscheuer1; 05-11-2013 at 07:41 PM. Reason: minor code tweak, add last lines of text
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Resolved Pass variable to php (ajax)
    By keyboard in forum Looking for such a script or service
    Replies: 5
    Last Post: 05-08-2013, 05:01 AM
  2. JavaScript local local time script 24 hr format?
    By eddiy in forum Dynamic Drive scripts help
    Replies: 1
    Last Post: 02-15-2013, 11:07 AM
  3. Not able to pass Javascript variable into PHP
    By zomb1e in forum JavaScript
    Replies: 0
    Last Post: 07-28-2012, 12:10 AM
  4. Replies: 7
    Last Post: 06-13-2011, 08:18 AM
  5. Replies: 1
    Last Post: 03-31-2006, 04:37 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •