## Using PHP to find distance between Zip Codes

This post is more than 18 months old. Since technology changes too rapidly, this content may be out of date (but that's not always the case). Please remember to verify any technical or programming information with the current release.

Today marked the second time I had to write this code from scratch. To save my self time - and hopefully you too! - I’m going to post what I’ve developed.

I found a great resource at ibegin.com - a download of 5 digit zip codes, city, state and county name, and their latitude and longitude. (Just in case it’s unavailable, I have archived it here.)

The table I’m using was created with this SQL:

``````CREATE TABLE  `zipgeo` (
`zip5` char(5) NOT NULL,
`city` varchar(250) NOT NULL,
`state` varchar(250) NOT NULL,
`lat` double NOT NULL,
`lon` double NOT NULL,
`county` varchar(250) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
``````

The following is the code used to import this .csv file into the table:

``````set_time_limit(0);
\$mysqlhost = 'localhost';
\$mysqluser = 'user';
\$mysqlpass = 'pass';
\$mysqldb = 'mydatabase';
\$mysqltable = 'zipgeo';
mysql_connect(\$mysqlhost, \$mysqluser, \$mysqlpass) or die(mysql_error());
mysql_select_db(\$mysqldb) or die(mysql_error());

\$fields = array('zip5', 'city', 'state', 'lat', 'lon', 'county');
\$contents = file('zip5.csv');

\$buffer = 100;
\$basestatement = "insert into {\$mysqltable} (`"
. implode("`, `", \$fields)
. "`) VALUES ";

\$counter = 0;
\$inserts = array();
foreach (\$contents as \$line) {
\$linefields = explode(',', \$line);
\$linefields = array_map('trim', \$linefields);
\$linefields = array_map('mysql_real_escape_string', \$linefields);
\$inserts[] = "('" . implode("', '", \$linefields) . "')";
\$counter++;

if (\$counter == \$buffer) {
\$query = \$basestatement . implode(',', \$inserts);
mysql_query(\$query) or die(mysql_error());
\$counter = 0;
\$inserts = array();
}
}

if (count(\$inserts)) {
\$query = \$basestatement . implode(',', \$inserts);
mysql_query(\$query);
}

print 'done';
``````

This imported a nice set of 41755 zip code rows.

### Distance Calculations

Now, I should give a disclaimer: this is just code that you can use. It is not the ‘cleanest’ or best organized. When I implement this code for my employer, I will be making a few changes, including it in a class, etc.

``````function degrees_difference(\$lat1, \$lon1, \$lat2, \$lon2)
{
\$theta = \$lon1 - \$lon2;

\$dist = acos(\$dist);

\$distance = \$dist * 60 * 1.1515;

return \$distance;
}
``````

This will return the distance in miles between one lat/long combination and another.

``````function difference_between(\$firstzip, \$secondzip)
{
\$query = "select zip5, lat, lon from zipgeo where zip5 in ({\$firstzip}, {\$secondzip})";
\$result = mysql_query(\$query) or die(mysql_error());

\$firstzips = mysql_fetch_array(\$result);
\$secondzips = mysql_fetch_array(\$result);

return degrees_difference(
\$firstzips['lat'],
\$firstzips['lon'],
\$secondzips['lat'],
\$secondzips['lon']
);
}
``````

This code gets the latitude and longitude for two zip codes and then executes that last function. Note: it doesn’t matter what order the zip codes are - the distance from A to B is always the same as B to A.

``````function get_zips_within(\$zip, \$miles)
{
\$milesperdegree = 69;
\$degreesdiff = \$miles / \$milesperdegree;

\$query = "select lat, lon from zipgeo where zip5={\$zip}";
\$result = mysql_query(\$query);
\$latlong = mysql_fetch_assoc(\$result);

\$lat1 = \$latlong['lat'] - \$degreesdiff;
\$lat2 = \$latlong['lat'] + \$degreesdiff;
\$lon1 = \$latlong['lon'] - \$degreesdiff;
\$lon2 = \$latlong['lon'] + \$degreesdiff;

\$query = "select * from zipgeo where lat between {\$lat1} and {\$lat2} and lon between {\$lon1} and {\$lon2}";

\$result = mysql_query(\$query);

\$zips = array();
while (\$row = mysql_fetch_assoc(\$result)) {
\$zips[] = \$row;
}

return \$zips;
}
``````

This last statement gets the zips within that many miles.

### How Should I Use These?

Because I’m not about to do the calculations based on the earth’s curvature in my SQL statement, I can have some misleading results. Since the distance - especially as the difference in location grows - is elongated by the curvature, the initial query using the between statement should actually request a larger mileage than expected. Then, this result set should be looped through and compared using the function which computes using the curvature to get a more accurate result set.

So, basically, if I were going to get all locations within 50 miles of 12345:

• Run query to get all results within 65 miles of 12345

• loop each result through next function which computes distance between 12345 and result’s zip. If it’s 50 or less, keep it.
{"ft_lang_label":"__label__en","ft_lang_prob":0.71992904,"math_prob":0.9591979,"size":4450,"snap":"2021-43-2021-49","text_gpt3_token_len":1206,"char_repetition_ratio":0.09739091,"word_repetition_ratio":0.018404908,"special_character_ratio":0.30674157,"punctuation_ratio":0.17571059,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96061224,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-17T15:21:16Z\",\"WARC-Record-ID\":\"<urn:uuid:57606592-a1e6-4b5f-8f70-6a1f7abafba1>\",\"Content-Length\":\"23600\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0716e7ed-4b16-46df-9b7d-34f79d4d572a>\",\"WARC-Concurrent-To\":\"<urn:uuid:b8032b89-8b9d-4337-a06c-9f4bfa76d61c>\",\"WARC-IP-Address\":\"172.67.190.28\",\"WARC-Target-URI\":\"https://www.aaronsaray.com/2009/using-php-to-find-distance-between-zip-codes\",\"WARC-Payload-Digest\":\"sha1:GPTE4WZW3QF2L5RDP4VVKKKREL726IES\",\"WARC-Block-Digest\":\"sha1:NJGUMIOKRECESEO5EB6BSNKDBS66SJLC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585178.60_warc_CC-MAIN-20211017144318-20211017174318-00011.warc.gz\"}"}