Jump to content

user's Local Time into a PW php variable


tesseract
 Share

Recommended Posts

My 1st post. 

I'd like to place the user's local time (obtained via javascript) into a ProcessWire php variable.  

I've tried using $session:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="<?php echo $config->urls->templates?>scripts/jquery-1.4.2.min.js"></script>

<script type="text/javascript">
     document.ready = function() {
         var visitortime = new Date();
         "<?php echo $session->time; ?>" + "=" + visitortime;     
    };
</script>
</head>

<body>
    <?php $t = $session->time; echo $t; ?>
</body>

</html>

and $input with a hidden form submitted by javascript:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="<?php echo $config->urls->templates?>scripts/jquery-1.4.2.min.js"></script>
   
<script type="text/javascript">
     document.ready = function () {
         var visitortime = new Date();
    document.getElementById("utoffset").setAttribute("value",visitortime);
     document.getElementById("utoffset").submit();
     }
    </script>
</head>

<body>
    <form action="." method="post">
    <input type="hidden" id="utoffset" name="vt">
    </form>
<?php echo $input->post->utoffset; ?>
</body>  

</html>

Both display blank pages (using the above .php files as templates).

Does anyone see what I'm missing? Or is there an easier way? Completely baffled.

Link to comment
Share on other sites

The first option won't work because php is server-side and runs before the page loads. 

The second option has potential, but you are echo'ing:

$input->post->utoffset;

when the name of your hidden field is "vt" and not "utoffset".

  • Like 2
Link to comment
Share on other sites

The problem is that you can echo PHP variables into Javascript but not the other way around - PHP is processed on the server before it's served to the browser - JavaScript is processed via the browser so it simply doesn't understand what you're doing.

In the first example, you've essentially typed " = visitortime"

EDIT: adrian beat me to it :)

There are a few different options on StackOverflow, but I like the PHP approach in this one best so far as - whilst it's not foolproof - it doesn't use Javascript so it can't be manipulated by the user: http://stackoverflow.com/questions/1718935/how-do-i-get-a-visitors-time-zone-in-php

  • Like 1
Link to comment
Share on other sites

You can not know the client time at the point when the server is first hit, it would require two pages/requests - the first to serve the javascript and the second to recieve the result of the javascript. That is why it's much eaiser to pass PHP time() to the page as a javascript variable and let the client-side handle the locale, if you need to syncronize.

<script type="text/javascript">
    var d = new Date(<?php echo time(); ?> * 1000);
    document.write(d.toLocaleString());
</script> 

If you must do it the other way around, you can redirect to the session - but it's a bag of worms. I'm using a non-processwire session to keep the example simple.

<?php
    session_start();
    if( !isset($_SESSION['client_time']) && isset($_GET['client_time']) ){
        $_SESSION['client_time'] = $_GET['client_time'];
    } 
?>

<script type="text/javascript">
    var server_time = '<?php echo addslashes($_SESSION['client_time']); ?>';
    if (server_time === ''){
        var d = new Date();
        window.location =  window.location + '?client_time='+encodeURI(d.toLocaleString());
    }
    document.write(server_time);
</script> 

EDIT: Beaten by Adrian and Pete, also good answers.

  • Like 2
Link to comment
Share on other sites

Before reading your replies, I wanted to send the user's local time back to php, then run a bunch of astronomical calculations on the server, then refresh the visitor's page.  But that's 2 reloads after the 1st one, and it looks suspicious when a page blinks twice. Ajax might work without refreshing, but it's beyond me.

So I've thought of a work-around by doing the .php calculations using UT time, then make adjustments in .js based on visitor's UToffset.

ProcessWire is awesome.

Thanks guys,

Gregory

www.1au.com

  • Like 3
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...