Lets calculate the distance between 2 zip codes. You need to first generate longitude and latitude from zip code and then you can calculate the distance between both longitude & latitude and display it in Kilometers or Miles.

### In Action:

 Enter Zip code1 Enter Zip Code2 Unit KM Miles

```// This function returns Longitude & Latitude from zip code.
function getLnt(\$zip){
\$result_string = file_get_contents(\$url);
\$result = json_decode(\$result_string, true);
\$result1[]=\$result['results'][0];
\$result2[]=\$result1[0]['geometry'];
\$result3[]=\$result2[0]['location'];
return \$result3[0];
}

function getDistance(\$zip1, \$zip2, \$unit){
\$first_lat = getLnt(\$zip1);
\$next_lat = getLnt(\$zip2);
\$lat1 = \$first_lat['lat'];
\$lon1 = \$first_lat['lng'];
\$lat2 = \$next_lat['lat'];
\$lon2 = \$next_lat['lng'];
\$theta=\$lon1-\$lon2;
\$dist = acos(\$dist);
\$miles = \$dist * 60 * 1.1515;
\$unit = strtoupper(\$unit);

if (\$unit == "K"){
return (\$miles * 1.609344)." ".\$unit;
}
else if (\$unit =="N"){
return (\$miles * 0.8684)." ".\$unit;
}
else{
return \$miles." ".\$unit;
}

}
```

HTML Code:
```<form method="POST" action="?flag=true">
<tr>

<td width="23%" height="34">Enter Zip code1</td>
<td width="28%" height="34">
<input type="text" name="zipCode" size="20"
value="<?php print \$_POST['zipCode']; ?>"></td>
<td height="34">&nbsp;</td>

</tr>
<tr>
<td width="23%" height="34">Enter Zip Code2</td>
<td width="28%" height="34">
<input type="text" name="zipCode2" size="20"
value="<?php print \$_POST['zipCode2']; ?>"></td>
<td height="34">&nbsp;</td>
</tr>
<tr>
<td width="23%" height="34">Unit</td>
<td width="28%" height="34">
<input type="radio" value="K" name="unit" checked> KM
<td height="34">&nbsp;</td>
</tr>
<tr>
<td width="23%" height="34">&nbsp;</td>
```\$distance = getDistance(\$_POST['zipCode'],\$_POST['zipCode2'],\$_POST['\$unit']);