Files
vedant-chavan/app/Http/Helpers/TimeZoneHelper.php

25 lines
773 B
PHP
Raw Permalink Normal View History

2024-06-12 20:29:05 +05:30
<?php
use GeoIp2\Database\Reader;
function getCountryTimeZone($userSelectedCountry)
{
// Path to the GeoIP2 database file
$databasePath = storage_path('app/GeoLite2-Country.mmdb'); // Replace with the path to your downloaded database file
// Initialize the GeoIP2 reader
$reader = new Reader($databasePath);
try {
// Look up the country for the user's selected IP address or country code
$record = $reader->country($userSelectedCountry);
// Get the time zone from the country data
$timeZone = $record->location->timeZone;
return $timeZone;
} catch (\Exception $e) {
// Handle exceptions if the country code or IP address is not found
return 'UTC'; // Default to UTC if not found
}
}