Hi all,
If anyone has the time to look at this messy code and give thier advice on how to make it sweater it would be helpful.
The code works but there must be a better way to write it.
The function
function secondsToTime($seconds, $return_type="string") {
// extract days
$days = floor($seconds / 3600 / 24);
// extract hours
$hours = floor($seconds / 3600) - $days*24;
// extract minutes
$divisor_for_minutes = $seconds % 3600;
$minutes = floor($divisor_for_minutes / 60);
// extract the remaining seconds
$divisor_for_seconds = $divisor_for_minutes % 60;
$seconds = ceil($divisor_for_seconds);
// return the final array
$obj = array(
"d" => (int) $days,
"h" => (int) $hours,
"m" => (int) $minutes,
"s" => (int) $seconds
);
$str = "";
if ($obj["d"]!=0) $str .= $obj["d"]."d ";
$str .= $obj["h"]."h ";
$str .= $obj["m"]."m ";
if ($return_type=="string") return $obj["h"].":".$obj["m"];
else return $obj;
}
The code inserts a leading zero if the the result of the function return minuets as a single digit; like "4:5" to "4:05"
$delaytime = ($row_flightLHR['EstStamp'] - $row_flightLHR['SchStamp']);
$mins = secondsToTime($delaytime);
$delayChunks = explode(":", $mins);
$digits = count(preg_split("/\d/", $delayChunks[1]))-1;
if($digits < 2) {
$minutes = sprintf("%02d", $delayChunks[1]);
print $delayChunks[0].":".$minutes;
} else {
print $mins ;
}
Many thanks.