How can PHP developers ensure accurate results when calculating distances between coordinates that span across different hemispheres?

When calculating distances between coordinates that span across different hemispheres, PHP developers can ensure accurate results by using the Haversine formula, which takes into account the curvature of the Earth. This formula accounts for the Earth's radius and the difference in latitude and longitude between the two points to calculate the distance accurately.

function haversineDistance($lat1, $lon1, $lat2, $lon2) {
    $earthRadius = 6371; // in kilometers

    $dLat = deg2rad($lat2 - $lat1);
    $dLon = deg2rad($lon2 - $lon1);

    $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * sin($dLon/2) * sin($dLon/2);
    $c = 2 * atan2(sqrt($a), sqrt(1-$a));

    $distance = $earthRadius * $c;

    return $distance;
}

// Example usage
$distance = haversineDistance(37.7749, -122.4194, 48.8566, 2.3522); // San Francisco to Paris
echo "Distance: " . $distance . " km";