/*
   ###########################################################################
   This function converts a HMS time string into equivalent decimal hours.

   INPUT: 1, 2 or 3-element HMS (Hours Minutes Seconds) separated by spaces
          in angular string as 'hrs min sec' values.

   Example: Given HMS string = '18 15 30.361'
            Returns:  18.2584336111111111

   NO DEPENDENCIES

   ERRORS:
   No error checking is done.
   ###########################################################################
*/

   function HMS_to_Hours ($HMSstr, $Decimals=16)
{
   $hms = StrToLower(trim($HMSstr));

   $Q = 40; // Internal working decimals.
   $q = trim($Decimals);

/* ----------------------------------------------------
   Normalize the spacing and then split and extract the
   individual time string elements (hh, mm,ss).
*/
   $hms = PReg_Replace("/\s+/", " ", trim($hms));
   $whms = PReg_Split("[ ]", $hms);
   $whmsCount = count($whms);
   $hh = ($whmsCount >= 1)? bcAdd($whms[0],'0', $Q) : '0';
   $mm = ($whmsCount >= 2)? bcAdd($whms[1],'0', $Q) : '0';
   $ss = ($whmsCount >= 3)? bcAdd($whms[2],'0', $Q) : '0';

/* -------------------------------------------------
   Remember and then remove any numerical (+/-) sign
   for reattachment to the returned output.
*/
   $NumSign = (substr($HMSstr,0,1) == '-')? '-' : '';
   $hh = Str_Replace('+', '', Str_Replace('-', '', $hh));

/* -------------------------------------------------------------
   If original angle argument began with a + sign, then preserve
   it so that all returned positive results will have a + sign.
   Otherwise, positive results will NOT have a + sign.
*/
   if (substr($HMSstr,0,1) == '+') {$NumSign = '+';}

/* ----------------------------------------------
   Compute decimal hours value equivalent
   to the given HHMS argument elements.
*/
   $w2 = bcAdd(bcAdd(bcMul($hh,"3600",$Q),bcMul($mm,"60",$Q),$Q),$ss,$Q);

// -----------------------------------------------------------
// If result equates to zero, then suppress any numerical sign.

   if (bcComp($w2, '0', $Q) == 0) {$NumSign = '';}

// -------------------------------------------------------
// Return result to (q) decimals, recalling original sign.

   $zeros = Str_Repeat('0', $q);
   $w = $NumSign . bcAdd(bcDiv($w2,"3600",$Q), '0.'.$zeros.'5', $q);
// $w = RTrim(RTrim($w, '0'), '.');

   return $w;

} // End of  HMS_to_Hours (...)