1

我正在尝试将十进制时间转换为具有小时和分钟的实际时间格式,即:以 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 中这样做。

4

6 回答 6

7

对于任何有兴趣的人...这就是您如何将十进制时间(其中 0.1 == 6 分钟)仅在 MYSQL 中转换为小时和分钟(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,因此前导零以个位数分钟计。

于 2016-01-09T01:34:41.423 回答
6

您可以在 SQL 语句中执行此操作,如下所示:

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

mydecimal 是您未格式化的字段名称

于 2014-08-22T11:33:46.433 回答
0

我想我已经计算出了你的时间价值……虽然这有点痛苦。

看来您的“十进制时间”是“小时.分钟”?相当可怕而且绝对不是一种好的格式:为了处理时间,最好坚持使用指定总分钟/秒/小时或您需要的任何粒度的整数。

但是假设它是 hours.minutes,你应该可以在 PHP 中这样做:

while($row = mysql_fetch_array($result))
{
$dec = $row['vluchttijddecimal'];
return sprintf("%2d:%2d", floor($dec), floor(($dec - floor($dec))*100));
}
于 2013-08-27T16:10:02.393 回答
0

希望我假设您的意思是正确的,例如 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;

看到它工作

于 2013-08-27T18:20:37.097 回答
0

这是我用于工资系统的:

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 分钟

于 2019-08-10T09:17:36.160 回答
0

我建议这包括秒。它基于@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;
于 2021-10-11T12:28:04.560 回答