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

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #28585

    I’m looking for a very simple (free) script for doing a zip code radius search.

    A user will input a zip code into a form field. I then need to check that provided zip code with a mysql database table of programs (which will have zipcode or lat/long, whatever needed, and only display those within 45 miles of the provided zip code.

    So essentially I just need help generating a mysql query and doing the math. I’ve been referenced to the greatest circle distance method but I’m having trouble translating it.

    Anybody have any good resources on this?


    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,


    Thanks. I have found a script online that’s helped me solved this.

    Thanks for the explanation.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Back End’ is closed to new topics and replies.