Converting Degree, Minutes, Seconds (DMS) to decimal in PHP

Building further on the DMS to decimal in PHP to obtain a representational string as seen on Google Maps.

Avatar

Converting geographical coordinates from Degree, Minutes, Seconds (DMS) format to decimal format is a common requirement in many applications involving mapping, GPS, and location-based services. PHP provides functions to easily perform this conversion, allowing developers to manipulate and display coordinates more conveniently. Building further on the DDtoDMS($dec) function, if you would like to obtain a representational string as seen on Google Maps, you could use the below function as an extension:

Understanding the Code

The given PHP code offers two essential functions:

  1. DMStoDD($deg, $min, $sec)
  2. DDtoDMS($dec)

The DMStoDD function converts DMS (Degrees/Minutes/Seconds) format to decimal format, while DDtoDMS does the opposite conversion—decimal to DMS.

 

Converting DMS to Decimal

The DMStoDD function takes three parameters: degrees, minutes, and seconds, and calculates the decimal value. It applies the formula:

This function adds the degrees to the result of converting minutes and seconds into fractions of a degree.

 

Converting Decimal to DMS

The DDtoDMS function performs the reverse operation. Given a decimal value, it calculates the degrees, minutes, and seconds.

This function extracts the degrees from the decimal value, converts the remaining decimal part into minutes and seconds, and returns an array containing these values.

 

Enhancing Functionality

Additionally, there’s an extended function DDtoDMS_string that converts coordinates into a representational string format commonly used in mapping services like Google Maps.

The function formats latitude and longitude into a human-readable string in DMS format, including North/South (for latitude) and East/West (for longitude) indicators.

 

Practical Usage

Here are some examples of using these functions to convert coordinates:

 

Conclusion

Converting geographical coordinates between DMS and decimal formats is crucial for various applications. The provided PHP functions offer an efficient way to perform these conversions, making it easier to handle and display location-based data. Whether for mapping services, GPS applications, or general coordinate manipulation, these functions facilitate seamless conversions between different coordinate representations in PHP.

 

Here is completed

 

 

 

 

Examples:

 

Or use it separately:

Gives you:

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *