我试图取一个小数并转换它,以便我可以回答它为小时,分钟和秒.
我有时间和分钟,但我正在试图找到秒钟.谷歌搜索了一段时间没有运气.我确信这很简单,但我尝试过的任何工作都没有.任何建议表示赞赏!
这是我有的:
function convertTime($dec)
{
$hour = floor($dec);
$min = round(60*($dec - $hour));
}
Run Code Online (Sandbox Code Playgroud)
就像我说的那样,我得到的时间和分钟没有问题.因某种原因只是努力争取秒数.
谢谢!
Cro*_*tab 22
如果$dec是在小时($dec由于提问者具体提及一个分解进制):
function convertTime($dec)
{
// start by converting to seconds
$seconds = ($dec * 3600);
// we're given hours, so let's get those the easy way
$hours = floor($dec);
// since we've "calculated" hours, let's remove them from the seconds variable
$seconds -= $hours * 3600;
// calculate minutes left
$minutes = floor($seconds / 60);
// remove those from seconds as well
$seconds -= $minutes * 60;
// return the time formatted HH:MM:SS
return lz($hours).":".lz($minutes).":".lz($seconds);
}
// lz = leading zero
function lz($num)
{
return (strlen($num) < 2) ? "0{$num}" : $num;
}
Run Code Online (Sandbox Code Playgroud)
一行非常简单的解决方案:
echo gmdate('H:i:s', floor(5.67891234 * 3600));
Run Code Online (Sandbox Code Playgroud)
小智 5
在我的情况下,所有upvoted都不起作用。我已经使用该解决方案将十进制小时和分钟转换为正常时间格式。IE
function clockalize($in){
$h = intval($in);
$m = round((((($in - $h) / 100.0) * 60.0) * 100), 0);
if ($m == 60)
{
$h++;
$m = 0;
}
$retval = sprintf("%02d:%02d", $h, $m);
return $retval;
}
clockalize("17.5"); // 17:30
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25537 次 |
| 最近记录: |