The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Zip Code Radius PHP Scripts Re: Zip Code Radius PHP Scripts


Because zip codes are on the globe, which is a sphere (i’m told), a circle distance won’t quite do it. Your problem is in 2 parts, and while I won’t just give you the code, I can tell you how to solve it.

First, you need to convert the zip codes to latitudes and longitudes. I suggest using the google geocoder. Once you have all of your codes in latitudes and longitudes, when a user enters their zip code, you need to convert that to a latitude and longitude as well.

Then, to find the distance, you need to use a formula called the haversine formula. It can take 2 latitudes and longitudes and give you the distance between them. So your sql should just get all zips where that distance is less than whatever you want.

Good luck,