User Tools

Site Tools


decoding_dates_and_times

Converting Decimal Hours Into an (hh, mm, ss) Time String.

Sometimes we have to convert a decimal fraction of a day or hours given as a decimal fraction into the corresponding days, hours, minutes and seconds values or vice versa. In PHP, this is very easy. Below are two random example programs the with related source code to demonstrate the process of converting decimal hours into (hh,mm,ss) and decimal days into (dd, hh,mm,ss).


Here is an example program showing how to convert a random decimal hours value into an (hh, mm, ss.sss) string

<?php

// Convert decimal hours to (hh, mm, ss.sss) using standard-precision
// arithmetic.  The seconds are rounded to from 0 to 3 decimals.

// Generate random values for example or substitute any desired custom values.
   $hours = ((mt_rand() % 2 == 0)? "":"-") . mt_rand(0, 23) . "." . mt_rand(0, 9) . mt_rand();

// Set random number of decimals in seconds part of output
// and construct output format control string.
   $D = mt_rand(0, 3); // Range 0 to 3

// -----------------------------------------------------
// Remember numerical sign and work with absolute value.
// The sign will be reattached to the final result.
   $sign = ($hours < 0)? "-" : "";  

// Derive hours value.
   $hh = floor(abs($hours));

// Derive minutes value.   
   $minutes = 60*(abs($hours) - $hh);
   $mm = floor($minutes);

// Derive seconds value.   
   $seconds = 60*($minutes - $mm);

// Format (hh, mm, ss) values for output.
   $hh  = sprintf("%02d",  $hh);
   $mm  = sprintf("%02d",  $mm);
   $ss  = sprintf("%1.$D" . "f", $seconds);  if($ss < 10) {$ss = "0$ss";}
   $hhh = $hh . "h";
   $mmm = $mm . "m";
   $sss = $ss . "s";

// Construct signed (±hh, mm, ss) time output strings.
   $hhmmss = "$sign$hh:$mm:$ss";
   $hhhmmmsss = "$sign$hhh $mmm $sss";
   
// Display the final (hh, mm, ss) result.
   print "<pre>Random Example:\n$hours hours\nDecimals = $D\n\nSo, we have:\n$hours hours = $hhhmmmsss  or  $hhmmss</pre>";

?>

Below is the output of the above (hours) to (hh, mm, ss) conversion example.

Random Example:
-1.61122865789 hours
ssDecimals = 3

So, we have:
-1.61122865789 hours = -01h 36m 40.423s  or  -01:36:40.423





Converting decimal (days) into a (dd, hh, mm, ss.sss) string

<?php

// Convert decimal days to (dd, hh, mm, ss.sss) using standard-precision
// arithmetic.  The seconds are rounded to from 0 to 3 decimals.

// Generate random days value for example or substitute any other custom value.
   $days = mt_rand(0, 365) . "." . mt_rand(0, 9) . mt_rand();

// Set number of decimals in seconds part of output
// and construct output format control string.
   $D=3; // Range 0 to 3

// Derive days (dd) value.
   $dd = floor($days);
   
// Derive hours value.    
   $hours = 24*($days - $dd);
   $hh = floor($hours);
   
// Derive minutes value.   
   $minutes = 60*($hours - $hh);
   $mm = floor($minutes);

// Derive seconds value
   $seconds = 60*($minutes - $mm);

// Format (dd, hh, mm, ss) values for output.
   $dd .= "d";
   $hh  = sprintf("%02d",  $hh) . "h";
   $mm  = sprintf("%02d",  $mm) . "m";
   $ss  = sprintf("%1.$D" . "f", $seconds);  if($ss < 10) {$ss = "0$ss";}
   $ss .= "s";

// Display the (dd,hh,mm,ss) result.
   print "<pre>$days days = $dd $hh $mm $ss</pre>";

?>

Below is the output of the above (days) to (dd, hh, mm, ss) conversion example.

295.21294451344 days = 295d 05h 06m 38.406s

decoding_dates_and_times.txt · Last modified: 2014/04/22 08:54 by Jay.Tanner.x@PHPScienceLabs.us