Skip to content

Instantly share code, notes, and snippets.

@tamboer
Last active December 14, 2015 21:39
Show Gist options
  • Save tamboer/5152971 to your computer and use it in GitHub Desktop.
Save tamboer/5152971 to your computer and use it in GitHub Desktop.
php function that takes a decimal value and format to hour and minutes
<?php
function formatToHourMinutes($decTime) {
$negative = false;
if(!is_numeric($decTime))return $decTime;
if($decTime == 0)return '-';
if($decTime < 0){
$negative = true;
$decTime = abs($decTime);//$decTime = -1 * abs($decTime);
}
$hour = floor($decTime);
$min = round(60 * ($decTime - $hour));
if($min !== 60) $time = sprintf('%02d:%02d', $hour, $min);
if($min == 60) $time = sprintf('%02d:%02d', $hour +1, '00');//to counter rounding of .9999999 to 60 minutes
//$time = sprintf('%02d:%02d', $hour, $min);
if ($time == '00:00')$time = '-';
if($negative == true)return '-'.$time;
return $time;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment