Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

get timezone in php

  • 16-07-2007 03:42PM
    #1
    Registered Users, Registered Users 2 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