PHP 5 Zip Code Range and Distance Calculation

PHP zipcode distance calculation

A silly little bit of code I wrote 6 years ago to calculate the distance between 2 zip codes in PHP based on a MySQL table still gets a lot of feedback to this day. About a year ago I was working on a project which needed this same functionality. I thought it would be a perfect opportunity to rewrite the code, bringing it up to date with PHP 5 syntax and implementing some perfomance improvements.

Things changed, my project went in a new direction, and I had forgotten about this code--until this morning. I found this little gem in my archives and sure enough, it's working. While there are some missing features and coding styles that I'm not in love with, it is certainly worth sharing with the PHP community rather than collecting dust in my halls of forgotten code.

Thus, I give you PHP-ZipCode-Class, a PHP 5 class for calculating distances and ranges on MySQL table containing zip codes.

See A Working Example

The Original Project

The original class, found on my blog post PHP Zip Code Range and Distance Calculation, was originally written for a client in 2005. Over the years there have been a couple of bug fixes, but ultimately I have abandoned it. It's PHP 4, it's ugly (IMO), and the calculations are much faster in the SQL queries than in the PHP code.

Missing Data

While the original database is included in the source code at Github, it's not even close to up-to-date. It's easy enough to use other databases, commercial or otherwise, by creating a MySQL view. If any of you out there know of a free and legal U.S. zip code database that is current please let me know. I'll try to get it included in the repo or at least add instructions on how to use it with the class.

Did you enjoy PHP 5 Zip Code Range and Distance Calculation? If you would like to help support my work, A donation of a buck or two would be very much appreciated.
blog comments powered by Disqus
Linux Servers on the Cloud IN MINUTES