Total members 11895 |It is currently Sat Dec 21, 2024 5:43 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Code:
<?php
$mysql_server 
= "mysql_server.com";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";

$ip = $_SERVER['REMOTE_ADDR'];
$ipno = Dot2LongIPv6($ip);

$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database"); 
// Connect to the IP2Location database
mysql_select_db("ip2location") or die("Could not select database"); 
// SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT time_zone, country_code FROM db11lite WHERE $ipno >= ip_from AND $ipno <= ip_to"; 
// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed"); 
// Retrieve the recordset (only one)
$row = mysql_fetch_object($result); 
// Keep the country information into two different variables
$country_code = $row->country_code;
$time_zone = $row->time_zone;

mysql_free_result($result); mysql_close($link);

$utc_time = gmdate("H:i:s");
$utc_h = explode(':', $utc_time);

if (strcmp($time_zone, "-") == 0) {
    $localdate = $utc_time;
}
else {
    $time = explode(':', $time_zone);

    if ($utc_h[0] + $time[0] < 0) {
        $hour = $utc_h[0] + 24 + $time[0];
    }
    elseif ($utc_h[0] + $time[0] >= 24) {
        $hour = $utc_h[0] + $time[0] - 24;
    }
    else {
        $hour = $utc_h[0] + $time[0];
    }
    
    $localdate 
= $hour . gmdate(":i:s");    
}

date_default_timezone_set("Asia/Kuala_Lumpur");
$date = date_create($localdate);
echo 'Country Code: ' . $country_code . '<br>';
echo 'Local Time: ' . date_format($date, "H:i:s"). '<br>';
echo 'Server Time: ' . strftime("%H:%M:%S") . '<br>';
echo 'UTC Time: ' . gmdate("H:i:s") . '<br>';

// Function to convert IP address to IP number (IPv6)
function Dot2LongIPv6 ($IPaddr) {
    $int = inet_pton($IPaddr);
    $bits = 15;
    $ipv6long = 0;
    while($bits >= 0){
        $bin = sprintf("%08b", (ord($int[$bits])));
        if($ipv6long){
            $ipv6long = $bin . $ipv6long;
        }
        else{
            $ipv6long = $bin;
        }
        $bits--;
    }
    $ipv6long = gmp_strval(gmp_init($ipv6long, 2), 10);
    return $ipv6long;
}
?>





Author:
Newbie
User avatar Posts: 4
Have thanks: 0 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Display Visitor's Local Time (IPv6 supported)
 use local user dll from website - usar dll local del usuario     -  
 log visitor IP address.     -  
 ban visitor from a specific IP-address     -  
 Local Web designers     -  
 VB/Oracle Developer-Must be local to Minnesota     -  
 Need help getting rowcount from local table using javascript     -  
 Calling a local variable from a static function     -  
 display list     -  
 SEVEN SEGMENT DISPLAY     -  
 Here is how to display any 2d array     -  



cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com