What mathematical models can be used to implement a PLZ Umkreis-Suche in PHP considering the uneven distribution of PLZ's?

The issue with implementing a PLZ Umkreis-Suche in PHP is that the postal codes (PLZ's) are unevenly distributed geographically, making it challenging to accurately calculate distances between them. One way to solve this is to use mathematical models such as the Haversine formula or the Vincenty formula for more precise distance calculations.

function calculateDistance($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;
}

$plz1_lat = 52.5200;
$plz1_lon = 13.4050;
$plz2_lat = 48.8566;
$plz2_lon = 2.3522;

$distance = calculateDistance($plz1_lat, $plz1_lon, $plz2_lat, $plz2_lon);
echo "Distance between PLZ 1 and PLZ 2 is: " . $distance . " km";