WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : greatcircle.php Convert latitude/longitude coordinates between degree and decimal format. Also has ability to determine distance between them.
Categories : Algorithms, PHP Click here to Update Your Picture
Doug Pillow
Date : Feb 27th 2003
Grade : 3 of 5 (graded 8 times)
Viewed : 24827
File : greatcircle.php
Images : No Images for this code example.
Search : More code by Doug Pillow
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples 
 

<?php
//degree format 121°8'6" degrees° minutes' seconds"
//decimal format 121.135
//accepts a coordinate in degree format and returns the equivalent decimal
//N & S are latitudes
//E & W are longitudes
//South latitudes and West longitudes are negative decimals
//calling examples
//degree2decimal('121°8‘6"N'); returns 121.135
//degree2decimal('121°8‘6"S'); returns -121.135
//degree2decimal('121°8‘6"E'); returns 121.135
//degree2decimal('121°8‘6"W'); returns -121.135
function degree2decimal($deg_coord="")
{
$dpos=strpos($deg_coord,'°');
$mpos=strpos($deg_coord,'‘');
$spos=strpos($deg_coord,'"');
$mlen=(($mpos-$dpos)-1);
$slen=(($spos-$mpos)-1);
$direction=substr(strrev($deg_coord),0,1);
$degrees=substr($deg_coord,0,$dpos);
$minutes=substr($deg_coord,$dpos+1,$mlen);
$seconds=substr($deg_coord,$mpos+1,$slen);
$seconds=($seconds/60);
$minutes=($minutes+$seconds);
$minutes=($minutes/60);
$decimal=($degrees+$minutes);
//South latitudes and West longitudes need to return a negative result
if (($direction=="S") or ($direction=="W"))
        { $decimal=$decimal*(-1);}
return $decimal;
}


//accepts a coordinate in decimal format and returns the equivalent degree
//as a string
//calling examples
//decimal2degree('121.135,'LAT'); will return 121°8‘6"N
//decimal2degree('-121.135,'LAT'); will return 121°8‘6"S
//decimal2degree('121.135,'LON'); will return 121°8‘6"E
//decimal2degree('-121.135,'LON'); will return 121°8‘6"W
function decimal2degree($decimal_coord="",$latorlon="")
{
//121.135
//degrees=121
//minutes=.135*60=(8).1
//seconds=.1*60=(6)
//121°8‘6"
$decpos=strpos($decimal_coord,'.');
$whole_part=substr($decimal_coord,0,$decpos);
$decimal_part=abs($decimal_coord-$whole_part);
$minutes=intval($decimal_part*60);
$seconds=intval((($decimal_part*60)-$minutes)*60);
if ($latorlon=='LAT')
        {if ($whole_part<0)
                {
                $whole_part=($whole_part*(-1));
                $L='S';
                }
        else
                {
                $L='N';
                }
        }//end if
else
        {if($latorlon=='LON')
                {if ($whole_part<0)
                        {
                        $whole_part=($whole_part*(-1));
                        $L='W';
                        }
                else
                        {
                        $L='E';
                        }
                }//end if
        }//end if
$degree=$whole_part.'°'.$minutes.'‘'.$seconds.'"';
$degree.=$L;
return $degree;
}

//accepts two decimal latitude, longitude coordinates and returns the distance
//between the two
function decimal_distance($lat1="",$lon1="",$lat2="",$lon2="")
{
//$radius is determined using the following formula
//(360 degrees)*(60 minutes per degree)*(1.852) km per minute
//give a circumference of 40003.2 km
//radius is circumference/(2*pi) which gives us 6637km or 3956miles
$radius=3956;
$lat1 = deg2rad ($lat1);
$lat2 = deg2rad ($lat2);
$lon1 = deg2rad ($lon1);
$lon2 = deg2rad ($lon2);
//Haversine Formula (from R.W. Sinnott, "Virtues of the Haversine",
//Sky and Telescope, vol. 68, no. 2, 1984, p. 159):
$dlon=$lon2-$lon1;
$dlat=$lat2-$lat1;
$sinlat=sin($dlat/2);
$sinlon=sin($dlon/2);
$a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlon*$sinlon);
$c=2*asin(min(1,sqrt($a)));
$d=$radius*$c;
//
return round($d,2);
}

//accepts two degree latitude, longitude coordinates and returns the distance
//between the two
function degree_distance($lat1="",$lon1="",$lat2="",$lon2="")
{
$lat1=degree2decimal($lat1);
$lat2=degree2decimal($lat2);
$lon1=degree2decimal($lon1);
$lon2=degree2decimal($lon2);
$distance=decimal_distance($lat1,$lon1,$lat2,$lon2);
return $distance;
}


?>

See it in action at http://www.geoarmy.com/distance_calculation/



A simple bubblesort that takes 2 arrays as argument.The first one is the actual data used for sorting, the second is data that will "tag along" with the first array, for instance a descriptive text about the data in the first array.
Categories : Algorithms, Arrays, PHP, Complete Programs
Object() = Custom __autoload + Singleton. "automagically" instantiates a class and always retuns the same instance of the same class. It's pretty useful when you want to have persistence in objects.
Categories : PHP, PHP Classes, Algorithms
How to validate an Israeli ID number.
Categories : Ecommerce, PHP, Algorithms
Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET
Categories : Algorithms, HTML and PHP, PHP, Variables
Function that returns an IP Address if it's correct. IMPROVED!!!
Categories : PHP, Algorithms, Network
Diffusion-Limited Aggregation visualization
Categories : PHP, Graphics, Algorithms, Math.
Password Creator: This PHP code exmaple shows how to use bitwise operations on a single variable and using it as a flagged variable. The class generates passwords of a given length using specified characters and the flags.
Categories : PHP, PHP Classes, Algorithms, Security
Reverse a given number
Categories : PHP, Beginner Guides, Algorithms, Math.
These 2 functions write and read the contents of a specially designated multi-dimensional array to and from a text file.
Categories : Algorithms, PHP, Filesystem
textwrap fill-paragraph (justification)
Categories : Strings, PHP, Algorithms
Calculate the great circle distance between two latitude/longitudes
Categories : Algorithms, PHP
A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Paginating the mySQL data
Categories : PHP, Algorithms, Databases, MySQL, HTML and PHP
SHA: Implementation of the Secure Hash Algorithm in pure PHP. This is a secure one-way function that can be used to perform challenge response login algorithms over an insecure connection.
Categories : Algorithms, PHP, Security
 Daniel Boos wrote :1249
//radius is circumference/(2*pi) which gives us 6637km or 3956miles

6637km is wrong it`s ~6367km