PHP-将十进制转换为小时,其中超过60分钟将被添加到小时中


PHP - Convert decimal into hours wherein exceeding 60 minutes will be added to hours


我有这个十进制值:

$decimalVal = 1.92

现在我想将其转换为时间(H:s(,另一个问题是在转换过程中,超过60分钟必须添加到小时中,剩下的分钟将被保留

$decimalVal = 1.92
.. some code here ..
// desired output is 02:32 (2 hours, 32 minutes)

我到处找,但似乎什么都不管用
请帮忙。谢谢

我在这里找到了答案。感谢LYF_HKN!

这是链接中的代码:

function convert($value) {
  if ( !preg_match('/^('d{1,2})'.('d{1,2})$/', $value, $matches) ||
      count($matches) !== 3)
    return 'convert: Invalid value provided';
  $matches[1] += floor($matches[2] / 60);
  return sprintf('%02d:%02d', $matches[1], $matches[2] % 60);
}
$decimalVal = 1.92;
// desired output is 02:32 (2 hours, 32 minutes)
echo convert($decimalVal);