TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Calculate Distance Between Two Points using PHP
Bhushan Singh
Oct 16
2014
Code
1.5
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
index.zip
<?php
function
my_getDistanceBetweenPoints(
$latitude1
,
$longitude1
,
$latitude2
,
$longitude2
) {
$theta
=
$longitude1
-
$longitude2
;
$miles
= (sin(
deg2rad
(
$latitude1
)) * sin(
deg2rad
(
$latitude2
))) + (
cos
(
deg2rad
(
$latitude1
)) *
cos
(
deg2rad
(
$latitude2
)) *
cos
(
deg2rad
(
$theta
)));
$miles
=
acos
(
$miles
);
$miles
= rad2deg(
$miles
);
$miles
=
$miles
* 60 * 1.1515;
$feet
=
$miles
* 5280;
$yards
=
$feet
/ 3;
$kilometers
=
$miles
* 1.609344;
$meters
=
$kilometers
* 1000;
return
compact(
'miles'
,
'feet'
,
'yards'
,
'kilometers'
,
'meters'
);
}
$point_A
=
array
(
'lat'
=> 40.770623,
'long'
=> -73.964367);
$point_B
=
array
(
'lat'
=> 40.758224,
'long'
=> -73.917404);
$show_distance
= my_getDistanceBetweenPoints(
$point_A
[
'lat'
],
$point_A
[
'long'
],
$point_B
[
'lat'
],
$point_B
[
'long'
]);
foreach
(
$show_distance
as
$unit
=>
$value
) {
echo
$unit
.
': '
.number_format(
$value
,4).
'<br />'
;
}
?>
calculate distance using php