Here is another delightful script that I used in real production at work — I needed to calculate the distance between two locations given their coordinates. I’m not strong in math, so I didn’t reinvent the wheel and simply used a very concise formula, adapting it for PHP:

<?php

$earthRadius = 6371; // Kilometers

$xLatitude = deg2rad(/** TODO */);
$xLongitude = deg2rad(/** TODO */);

$yLatitude = deg2rad(/** TODO */);
$yLongitude = deg2rad(/** TODO */);

$longitudeDelta = $xLongitude - $yLongitude;

$angle = atan2(
    sqrt(
        pow(cos($xLatitude) * sin($longitudeDelta), 2) +
        pow(cos($yLatitude) * sin($xLatitude) - sin($yLatitude) * cos($xLatitude) * cos($longitudeDelta), 2)
    ),
    sin($yLatitude) * sin($xLatitude) + cos($yLatitude) * cos($xLatitude) * cos($longitudeDelta)
);

echo $angle * $earthRadius;