PHP에서 Timezone_location_get ()은 지정된 시간대에 대한 지리적 위치 정보를 얻는 매우 실용적인 기능입니다. 시간대 데이터 또는 시간대 구역 관련 정보를 표시하는 시나리오에 종종 사용됩니다. 이 기사는 Timezone_location_get () 기능의 반환 데이터 형식을 자세히 분석하고 이러한 데이터를 효율적으로 처리하는 방법을 탐색합니다.
timezone_location_get () 함수는 datetimezone 객체를 매개 변수로 받아들이고 시간대에 해당하는 지리적 위치 정보 배열을 반환합니다. 특정 사용량은 다음과 같습니다.
$timezone = new DateTimeZone('Asia/Shanghai');
$location = timezone_location_get($timezone);
print_r($location);
함수는 다음과 유사한 구조와 관련된 어레이를 반환합니다.
Array
(
[country_code] => CN
[latitude] => 31.22222
[longitude] => 121.45806
[comments] => Shanghai
)
Country_Code : Country ISO 3166-1 Alpha-2 두 글자 코드.
위도 : 위도, 부동 소수점 번호,이 시간 구역의 중심 지점의 도의 단위.
경도 : 경도, 부동 소수점 수,이 시간대의 중심 지점에서 단위.
의견 : 시간대에 대한 간단한 설명, 일반적으로 주요 도시 이름.
실제 응용 분야에서, 이들 데이터의 추가 처리는 종종 다음과 같이 필요하다.
좌표 형식 변환 (학위와 분 및 두 번째 사이의 변환)
위도와 경도에 따라 거리를 계산합니다
국가 코드를 기반으로 국가 이름 또는 로고 검색
function decimalToDMS($decimal) {
$degrees = floor($decimal);
$minutes = floor(($decimal - $degrees) * 60);
$seconds = round((($decimal - $degrees) * 60 - $minutes) * 60, 2);
return "{$degrees}°{$minutes}'{$seconds}\"";
}
$latDMS = decimalToDMS($location['latitude']);
$lngDMS = decimalToDMS($location['longitude']);
echo "Latitude: $latDMS, Longitude: $lngDMS";
function haversineDistance($lat1, $lng1, $lat2, $lng2) {
$earthRadius = 6371; // 지구의 반경,단위는 킬로미터입니다
$dLat = deg2rad($lat2 - $lat1);
$dLng = deg2rad($lng2 - $lng1);
$a = sin($dLat / 2) * sin($dLat / 2) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
sin($dLng / 2) * sin($dLng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
return $earthRadius * $c;
}
// 예:상하이와 베이징 사이의 거리를 계산하십시오
$beijing = ['latitude' => 39.9042, 'longitude' => 116.4074];
$distance = haversineDistance($location['latitude'], $location['longitude'], $beijing['latitude'], $beijing['longitude']);
echo "상하이와 베이징 사이의 거리는 거의 없습니다 {$distance} 킬로미터";
Country_Code는 간단한 배열 또는 타사 라이브러리를 통해 전체 국가 이름으로 변환 할 수 있습니다.
$countryNames = [
'CN' => '중국',
'US' => '미국',
'JP' => '일본',
// 요구 사항에 따라 확장 할 수 있습니다
];
$country = $countryNames[$location['country_code']] ?? '알 수없는 국가';
echo "시간대의 국가:$country";
timezone_location_get () 함수가 제공하는 지리적 정보는 간단하고 직관적이며 시간대에 해당하는 지리적 좌표 및 국가 코드를 신속하게 얻는 데 적합합니다. 간단한 좌표 변환과 지리적 계산을 결합하면 다양한 지리적 정보 표시 및 처리 요구 사항을 충족 할 수 있습니다.
<?php
$timezone = new DateTimeZone('Asia/Shanghai');
$location = timezone_location_get($timezone);
function decimalToDMS($decimal) {
$degrees = floor($decimal);
$minutes = floor(($decimal - $degrees) * 60);
$seconds = round((($decimal - $degrees) * 60 - $minutes) * 60, 2);
return "{$degrees}°{$minutes}'{$seconds}\"";
}
$latDMS = decimalToDMS($location['latitude']);
$lngDMS = decimalToDMS($location['longitude']);
echo "시간대 지리 정보:\n";
echo "국가 코드: " . $location['country_code'] . "\n";
echo "위도: $latDMS\n";
echo "경도: $lngDMS\n";
echo "주목: " . $location['comments'] . "\n";
// 예:다른 위치에서 거리를 계산하십시오
function haversineDistance($lat1, $lng1, $lat2, $lng2) {
$earthRadius = 6371;
$dLat = deg2rad($lat2 - $lat1);
$dLng = deg2rad($lng2 - $lng1);
$a = sin($dLat/2) * sin($dLat/2) +
cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
sin($dLng/2) * sin($dLng/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
return $earthRadius * $c;
}
$beijing = ['latitude' => 39.9042, 'longitude' => 116.4074];
$distance = haversineDistance($location['latitude'], $location['longitude'], $beijing['latitude'], $beijing['longitude']);
echo "상하이와 베이징 사이의 거리는 거의 없습니다 {$distance} 킬로미터\n";