Last active
December 17, 2019 15:37
-
-
Save MartinTale/8219c37d12c29ff8de4762b61ad5b567 to your computer and use it in GitHub Desktop.
PHP function that converts float number to time..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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