mysql将十进制时间转换为xx:xx格式


mysql convert decimal time to xx:xx format

我正试图将十进制时间转换为以小时和分钟为单位的实际时间格式,即:以xx:xx小时为单位。

我的问题是:

select SUM(vt.vluchtdec) AS vluchttijddecimal
from tbl_vluchtgegevens vg
left join tbl_vluchttijd vt
on vg.vluchttijddec = vt.vluchttijdID
WHERE vg.vertrekdatum <=NOW();

我在回应

. $row['vluchttijddecimal'] .

我也尝试过,但这仍然以十进制格式给出了我的响应:

$result = mysql_query("select SUM(vt.vluchtdec) AS vluchttijddecimal
from tbl_vluchtgegevens vg
left join tbl_vluchttijd vt
on vg.vluchttijddec = vt.vluchttijdID
WHERE vg.vertrekdatum <=NOW();");
while($row = mysql_fetch_array($result))
{
$dec = $row['vluchttijddecimal'];
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;
}
echo "" .$dec."";

在MS Access中,我会这样做:

CInt([vluchttijddecimal]) & ":" & Format([vluchttijddecimal]*60 Mod 60;"00")

但这不起作用,或者我不知道如何在MySQL/php中做到这一点。

对于任何感兴趣的人。。。这就是在MYSQL中将十进制时间(其中0.1==6分钟)转换为小时和分钟(0.2333==14分钟)的方法。不需要PHP。这也说明了需要将秒舍入为分钟。

SELECT CONCAT(FLOOR(timeInDec),':', LPAD(ROUND((timeInDec - FLOOR(timeInDec)) * 60) % 60,2,0)) AS TimeInHoursMinutes
FROM YourTable;

将timeInDec替换为包含要转换的十进制时间的列名。

这将为0.1000的十进制值返回0:06,因此前导零以个位数分钟计。

您可以在SQL语句中这样做:

SELECT CONCAT(CEIL(mydecimal),':', LPAD(Floor(mydecimal*60 % 60),2,'0')) as formated text

其中mydecimal是未格式化的字段名

我想我已经计算了你的时间值。。。虽然有点疼。

您的"十进制时间"似乎是"小时.分钟"?相当可怕,而且绝对不是一个好的格式:对于处理时间,最好使用指定总分钟/秒/小时或您需要的任何粒度的整数。

但是假设它是小时.分钟,你应该能够在PHP中这样做:

while($row = mysql_fetch_array($result))
{
$dec = $row['vluchttijddecimal'];
return sprintf("%2d:%2d", floor($dec), floor(($dec - floor($dec))*100));
}

希望我假设你的意思是正确的,例如2.5小时=2H 30分钟。如果是,那么您的"时间"是一个时间间隔,最好用DateInterval类表示。

此功能将执行您想要的操作:-

/**
 * Converts a 'decimal time' in the format 1.5hours to DateInterval object
 * 
 * @param Int $decimalTime
 * @return DateInterval
 */
function decTimeToInterval($decimalTime)
{
    $hours = floor($decimalTime);
    $decimalTime -= $hours;
    $minutes = floor($decimalTime * 60);
    $decimalTime -= ($minutes/60);
    $seconds = floor($decimalTime * 3600);
    $interval = new 'DateInterval("PT{$hours}H{$minutes}M{$seconds}S");
    return $interval;
}
echo decTimeToInterval(512.168)->format("%H:%I:%S");

看到它工作

如果你想以"H:i"格式添加时间,而不将其转换为小数和小数,你可以这样做:-

function sumTimes($time1, $time2)
{
    list($hours1, $minutes1) = explode(':', $time1);
    list($hours2, $minutes2) = explode(':', $time2);
    $totalHours = $hours1 + $hours2;
    $totalMinutes = $minutes1 + $minutes2;
    if($totalMinutes >= 60){
        $hoursInMins = floor($totalMinutes/60);
        $totalHours += $hoursInMins;
        $totalMinutes -= ($hoursInMins * 60);
    }
    return "$totalHours:$totalMinutes";
}
echo sumTimes('12:54', '100:06') . PHP_EOL;
echo sumTimes('12:54', '100:20') . PHP_EOL;

查看它的工作

这是我在工资系统中使用的:

SELECT If(total_late>0, LPAD(CONCAT(REPLACE(FLOOR(total_late/60) + FORMAT(total_late%60*0.01,2), '.', ':'), ':00'), 8, 0), '00:00:00') FROM MyTable

我把它乘以0.01,因为我的变量是以秒为单位的。例如60.00=1分钟

我建议这包括秒。它基于@Richard的解决方案。请注意,在@Richard的解决方案中,我已按FLOOR更改了CEIL。

SET @timeInDec=1.505;
SELECT CONCAT(FLOOR(@timeInDec),':', LPAD(FLOOR(@timeInDec*60 % 60),2,'0'),':', LPAD(FLOOR(MOD(@timeInDec*60 % 60,1)*100),2,0)) as timeInDec;