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

Technologies

1. Nodejs
  • Express
  • Loopback
  • SailsJs
2. Angular
  • 2
  • 4
  • 5
  • 8
3. JQuery
4. PHP
  • LAMP
  • Codeigniter
  • Lumen
5. Database
  • MySQL
  • PostgreSQL
  • MongoDB
6. Artificial Intelligence
  • IBM Watson API
7. Amazon
  • S3
  • EC2
  • SES
  • ELB
8. REST & SOAP API integrations
9. Push notifications
  • FCM - Android
  • APNS - IOS
10. RETS(Real estate domain) servers
11. Standard Implementations
  • 508
  • LTI