Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get the user's time zone using PHP and Javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.20k
    Comment on it

    Hello Reader! If you developing the website for a global purpose then getting the time zone of your user is a important factor.
    In the example below we'll see how to get the user's time zone by using JavaScript and PHP with a custom webpage.

    First get the jquery file (The latest one) and call it on header <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

    Now write this is custom Jquery just below it:-

    <script type="text/javascript">
        $(document).ready(function() {
            if("<?php echo $timezone; ?>".length==0){
                var visitortime = new Date();
                var visitortimezone = "GMT " + -visitortime.getTimezoneOffset()/60;
                $.ajax({
                    type: "GET",
                    url: "http://abc.com/date.php",
                    data: 'time='+ visitortimezone,
                    success: function(){
                        location.reload();
                    }
                });
            }
        });
    </script>
    

    Where abc.com is your website and date.php is our next page

    Now the last step is to create date.php in the location as in url above

    <?php
        session_start();
        $_SESSION['time'] = $_GET['time'];
    ?>
    

    Now it will return the user's timezone.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: