Skip to content

Instantly share code, notes, and snippets.

@MartinTale
Last active December 17, 2019 15:37
Show Gist options
  • Save MartinTale/8219c37d12c29ff8de4762b61ad5b567 to your computer and use it in GitHub Desktop.
Save MartinTale/8219c37d12c29ff8de4762b61ad5b567 to your computer and use it in GitHub Desktop.
PHP function that converts float number to time..
<?php
if (! function_exists('floatToTime')) {
function floatToTime($time, $long = false)
{
$minutes = fmod($time, 1) * 60;
$units = [
'm' => 'm',
'h' => 'h',
];
if ($long) {
$units = [
'm' => ' minute',
'h' => ' hour',
];
}
if ($time < 1) {
return $minutes . str_plural($units['m'], $minutes);
} else {
if (empty($minutes)) {
return (int) $time . str_plural($units['h'], (int) $time);
} else {
return (int) $time . str_plural($units['h'], (int) $time) . ' ' . $minutes . str_plural($units['m'], $minutes);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment