Let me explain with an example.

I have a postcode NW1 5LR and its LATITUDE:51.546069024064 LONGITUDE:-0.235949008191785 coming from user entry. I also have many other postcodes and their Latitide&Longitide values in database. What formula should I use to get only those postcodes which are in 1 mil away maximum?


Looks like I will use a SQL statement to calculate and return the places instead of doing same thing in PHP.

Another question is: The code below is for 1 MIL, What is I want in 1 KM?

SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + 
COS($lat * PI() / 180) * COS(lat * PI() / 180) * 
COS(($lon – lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515)
AS `distance` 
FROM `table_name` 
HAVING `distance` => '1' 
ORDER BY `distance` ASC

Apologised if not relevant to PHP. I know it is more like SQL thread but I though it would be a PHP thread.


Member Avatar

That SQL doesn't look right to me.

HAVING `distance` => '1' 

Should be

HAVING `distance` >= '1' 


1 mile = 1.609344 kilometres

I don't understand how this query knows that value 1 represents MILE or KM?

Member Avatar

Well, it's beyond me. I'd have to sit down and think, wipe the nosebleed, sit down and think, recover from an stroke, sit down and think...

Seriously, there must be a script out there somewhere.

Last bit shoul be * 60 * 1.1515 * 1.609344 for KMs