我让我网站的用户在00:00-23:59时间文本输入中填写时间。然后我将它解析为双(例如,他们进入2:30,它将变为2.30)。我如何将这些时间改为实际的双打。说2:30 - > 2.50,1:45 - > 1.75等......我一直在考虑这个问题,我觉得答案很简单,但我似乎无法想到它。
谢谢!
答案 0 :(得分:1)
如下功能' convertTime'?这对你有用吗?
class TimeTest extends \PHPUnit_Framework_TestCase
{
function test_convertTime()
{
$this->assertEquals(23.0,convertTime(23.00));
$this->assertEquals(23.5,convertTime(23.30));
$this->assertEquals(23.17,round(convertTime(23.10),2));
$this->assertEquals(24.0,convertTime(23.60));
}
}
function convertTime($time){
$secondsPart = ($time - floor($time))*100;
if($secondsPart == 0){
return $time;
}
$secondsPartAsFraction = 60/$secondsPart;
$inDecimal = 100/$secondsPartAsFraction;
return floor($time)+($inDecimal/100);
}