Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

get timezone in php

Options
  • 16-07-2007 3:42pm
    #1
    Registered Users Posts: 648 ✭✭✭


    hi
    i want to have a page with the users current time and then a list of other cities with all thier times (in thier different time zones)

    what im doing to get the clients is timezone is (i believe only way is via js?)

    <SCRIPT TYPE="text/javascript">
    <!--
    var clientGMT=( (new Date()).getTimezoneOffset()*60)*(-1) ;//client timezone offset in seconds
    //-->

    </SCRIPT>

    however i need to use this in php code (in a caculation) how can i for example use the above variable in a php calculation like this :

    echo date('H:i:s',time()+$clientGMT-$serverGMT);



    tnx


Comments

  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    What you're asking for isn't possible; the JavaScript is only evaluated on the client, whereas the PHP is evaluated before the page arrives at the client.

    You'd presumably know the timezone that the server is in, but if people are viewing the site from a variety of worldwide locations, the only place to evaluate this is on the client - i.e. using their PC's clock.

    So if the PHP code is required for a database query or other server-side operation, it isn't possible in a single step; the only thing you could do is either

    1) Load a page that does the calculation and then performs another query via a redirect, refresh or load
    2) Do a document.write (if it's only an echo like the one above)
    3) Call/load a page section via AJAX

    For the echo, it's simple enough, though.

    <script type="text/javascript">
    var serverGMT = <?php echo date(FORMAT PARAMETERS TO MAKE IT THE SAME AS JAVASCRIPT'S DATE); ?>;
    var clientGMT = WHATEVER;
    document.write(JAVASCRIPT CALCULATION);
    </script>


Advertisement