save to codehub
This commit is contained in:
25
app/Http/Helpers/TimeZoneHelper.php
Normal file
25
app/Http/Helpers/TimeZoneHelper.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user