Setting up the Database

There are 6 files in the /sql directory which contain SQL statements intended for a MySQL database (though they should work just fine for any SQL database). The zip code database consists of over 40,000 records which would be too large a file for many configurations of phpMyAdmin. Therefore, I have broken the data into records of 10,000 resulting in 5 files. In phpMyAdmin, you can import these 6 files 1 at a time using the 'Import' tab. You MUST import the 'create_table.sql' file first, then each of the data files.

create_table.sql
data_1.sql
data_2.sql
data_3.sql
data_4.sql
data_5.sql

The database has changed since the last version. The data itself is comprised of data from multiple sources starting with the 2000 and 2004 US Census data. Unlike version 1.x of this class, the database is no longer comprised of a state AND zip_code table, but a single zip_code table. Additionally, the table is created with an index on the zip_code column.

The Class: zipcode.class.php

Constants

// constants for setting the $units data member
define('_UNIT_MILES', 'm');
define('_UNIT_KILOMETERS', 'k');

// constants for passing $sort to get_zips_in_range()
define('_ZIPS_SORT_BY_DISTANCE_ASC', 1);
define('_ZIPS_SORT_BY_DISTANCE_DESC', 2);
define('_ZIPS_SORT_BY_ZIP_ASC', 3);
define('_ZIPS_SORT_BY_ZIP_DESC', 4);

// constant for miles to kilometers conversion
define('_M2KM_FACTOR', 1.609344);

Data Members

decimals Read/Write. When returning distance values, this variable determines how many decimal places the result should be rounded to.
last_error Read-only. Holds a string value of the last error that occured. This can be used should a method return an error value to get a human readable version of the error. 
last_time Read-only. Holds the last method execution time used to debug the efficiency of the get_zips_in_range() method.
units Read/Write. Holds the type of units to show results in. Must be either _UNITS_MILES or _UNITS_KILOMETERS

Methods

calculate_mileage($lat1, $lat2, $lon1, $lon2) Calculates the mileage between the two lattitude/longitude points: $lat1/$lon1 and $lat2/$lon2.
chronometer() Chronometer function from PHP manual used to time execution of get_distance() and get_zips_in_range() methods.
get_distance($zip1, $zip2) Returns the distance between the two zip codes: $zip1 and $zip2. If there is an error, false is returned. Since the function can return 0 when 2 zips are the same, you must evaluate for an error condition using:
if (get_distance($zip1, $zip2) === false) // error condition
get_zip_details($zip) Returns the details about the zip code: $zip. Details are in the form of a keyed array. The keys are: latitude, longitude, city, county, state_prefix, state_name, area_code, and time_zone. All are pretty self-explanitory. Returns false on error.
get_zip_point($zip) Gets the lattitude and longitude coordinates of the given zip code as a keyed array. The keys are: lat and lon. Returns false on error.
get_zips_in_range($zip, $range, $sort,$include_base) Finds all zip codes within $range from $zip as a keyed array sorted by $sort. If $include_base is set to true or not specified, then $zip is included in the results. If it is set to false, then $zip is excluded from results. Possible values for $sort are: _ZIPS_SORT_BY_DISTANCE_ASC (default), _ZIPS_SORT_BY_DISTANCE_DESC, _ZIPS_SORT_BY_ZIP_ASC, _ZIPS_SORT_BY_ZIP_DESC. The return value is a keyed array where the key is the zip code and the value is the distance from the base zip $zip. Returns false on error.