Resham Deshpande
Resham Deshpande

Reputation: 49

How do I find user timezone in php?

How can I dynamically find user timezone in config.php file in codeigniter?

date_default_timezone_set('Asia/Kolkata');

Upvotes: 2

Views: 12594

Answers (3)

Rahul Gandhi
Rahul Gandhi

Reputation: 19

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

This will read the session variable "time", which we are now about to create.

On the same page, in the <head> section, first of all you need to include jQuery:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

Also in the <head> section, paste this jQuery:

<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://domain.com/timezone.php",
            data: 'time='+ visitortimezone,
            success: function(){
                location.reload();
            }
        });
    }
});
</script>

You may or may not have noticed, but you need to change the url to your actual domain.

One last thing. You are probably wondering what the heck timezone.php is. Well, it is simply this: (create a new file called timezone.php and point to it with the above url)

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

If this works correctly, it will first load the page, execute the JavaScript, and reload the page. You will then be able to read the $timezone variable and use it to your pleasure! It returns the current UTC/GMT time zone offset (GMT -7) or whatever timezone you are in.

Upvotes: 0

Deep Kakkar
Deep Kakkar

Reputation: 6305

Using Javascript you can get the user timezone:

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js">
</script>
<script type="text/javascript">
  $(document).ready(function(){
    var tz = jstz.determine(); // Determines the time zone of the browser client
    var timezone = tz.name(); //'Asia/Kolhata' for Indian Time.

    document.getElementById("hiddenVal").value = timezone;
 </script>

After getting timezone in javascript you can use that value in php code. In HTML make an hidden element with name and id as "hiddenval" and get value from that element on form submit like

<?hpp
 echo$usertime_zone =  $_REQUEST['hiddenval'];
?>

In case if you only want to use PHP then utilize the following code:

<?php
$ip     = $_SERVER['REMOTE_ADDR']; // means we got user's IP address 
$json   = file_get_contents( 'http://smart-ip.net/geoip-json/' . $ip); // this one service we gonna use to obtain timezone by IP
// maybe it's good to add some checks (if/else you've got an answer and if json could be decoded, etc.)
$ipData = json_decode( $json, true);

if ($ipData['timezone']) {
    $tz = new DateTimeZone( $ipData['timezone']);
    $now = new DateTime( 'now', $tz); // DateTime object corellated to user's timezone
} else {
   // we can't determine a timezone - do something else...
}

Upvotes: 2

drodil
drodil

Reputation: 2326

You must either use javascript (for example http://momentjs.com/timezone/) or then you must use the geoip to determine it in the backend side (CURL the client ip to http://freegeoip.net/?q=IP_ADDR). Also if you have multiple TLDs (for example .com, .se etc.) you can use those if nothing else works. Of course you should allow the user to change the timezone from user interface in case the automatic way fails for some reason.

Upvotes: 2

Related Questions