将值(作为持续时间)转换为小时JS / PHP的浮点数

问题描述 投票:0回答:1

我有一个数据库表,其中一个字段 - “持续时间” - 是FLOAT类型而不是TIME,即3.5小时而不是“03:30:00”。这是出于DB性能原因和编程简易性而完成的。出于UI原因,我正在将我的前端表单字段更新为

<input id="duration" type="time">

它产生TIME格式的值。有没有将TIME转换为FLOAT的简便方法?

到目前为止,我在想类似的东西

function timeToFloat(){

    var buff = $('#duration').val().split(':'); //to array
    buff.pop(); //remove seconds
    return parseInt(buff[0]) + parseFloat( buff[1] / 60 ).toFixed(2);

}

在JS或PHP中:

function timeToFloat( $duration ){

     $duration = explode(":", $duration);
     array_pop($duration);
     return $duration[0] + numberFormat( $duration[1] / 60, 2 );

}

谁知道更好?

javascript php jquery html5 forms
1个回答
1
投票

只需在MySql中执行此操作即可

SELECT TIME_TO_SEC('03:30:00')/3600; 3.5000

© www.soinside.com 2019 - 2024. All rights reserved.