Files
gsf/app/Http/Helpers/TimeZoneHelper.php
vedant-chavan 20f55281ef save to codehub
2024-08-09 17:11:41 +05:30

25 lines
773 B
PHP

<?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
}
}