Get Client's Accurate date time using IP Address in PHP even if the client set the time incorrectly
This is about getting the client machine's(Location) current date time, in the PHP server (Using ajax request) by using client's IP Address even if he/she sets the time incorrectly. Here I am using two functions -
geoip_record_by_name(),
geoip_time_zone_by_country_and_region().
In order to use these functions first need to install some libraries in PHP. The installations procedure is here.
<php
$clientIP = $_SERVER['REMOTE_ADDR']; //Client Machine IP (Should be a Public Host)
$region = geoip_record_by_name($clientIP);
if (is_array($region)) {
$timezone = geoip_time_zone_by_country_and_region($region['country_code'],$region['region']);
date_default_timezone_set($timezone);
$date = new DateTime(date('Y-m-d H:i:s'), new DateTimeZone($timezone));
echo "Client's Current Date Time:\t". $date->format('Y-m-d H:i:s a T') . "\n";
}
?>
No comments:
Post a Comment