Hi, I am in need of some help. I'm currently working on some code that allows me to determine the zip codes within a certain radius of a user-given zip code. In my DB I have close to 4000 records of zip codes and their city, state, longitude, and latitude.
My problem is that I am having to query the DB for each zip code and then run the function below to determine it's proximity to the given zip code. Obviously, this takes quite some time. It eventually gives me the maximum execution timeout error. I don't want to increase the execution time. I am hoping someone out there is smart enough to possibly provide a solution. I tried limiting it to a singular state, but this is not optimal if someone provides a zipcode close to a state border.
The function I am using for determining the # of miles between the two zipcodes is:
function distance($lat1, $lon1, $lat2, $lon2, $unit)
{
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
return $miles;
}
Thanks for any help/thoughts you can offer.