如何将从 sql 查询返回的平均时间转换为分钟
How to convert average time returened from sql query to minutes
我是运行下面的查询
SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(Response_Time))) as avg_time
FROM Case_report
where Reporting_Time between "'.$fromdate.'" and "'.$todate.'"
and Police_Circle="Sadar"
并从 sql 得到正确的结果为“00:19:35.6000”
我需要在总分钟数中得到结果喜欢 20
您可以在 PHP 中使用一个简单的函数,例如:
function getMinutes($timestring){
$tmp = explode(':',str_replace('.','',$timestring));
$minutes = (int)$tmp[0] * 60 + ceil($tmp[1].'.'.$tmp[2]);
return $minutes;
}
$s = '00:19:35.6000';
$s2 = '01:19:35.6000';
echo getMinutes($s);
echo PHP_EOL;
echo getMinutes($s2);
输出:
20
80
在 SQL 的这一部分,您将总秒数转换为时间,这就是为什么您将输出作为时间:
SEC_TO_TIME(AVG(TIME_TO_SEC(Response_Time)))
无需将结果转换回时间,只需将秒数除以 60 并四舍五入即可得到总分钟数:
ROUND(AVG(TIME_TO_SEC(Response_Time)) / 60)
在您最初的 SQL 声明中:
SELECT ROUND(AVG(TIME_TO_SEC(Response_Time)) / 60) AS avg_time
FROM Case_report
where Reporting_Time between "'.$fromdate.'" and "'.$todate.'"
and Police_Circle="Sadar"
我是运行下面的查询
SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(Response_Time))) as avg_time
FROM Case_report
where Reporting_Time between "'.$fromdate.'" and "'.$todate.'"
and Police_Circle="Sadar"
并从 sql 得到正确的结果为“00:19:35.6000”
我需要在总分钟数中得到结果喜欢 20
您可以在 PHP 中使用一个简单的函数,例如:
function getMinutes($timestring){
$tmp = explode(':',str_replace('.','',$timestring));
$minutes = (int)$tmp[0] * 60 + ceil($tmp[1].'.'.$tmp[2]);
return $minutes;
}
$s = '00:19:35.6000';
$s2 = '01:19:35.6000';
echo getMinutes($s);
echo PHP_EOL;
echo getMinutes($s2);
输出:
20
80
在 SQL 的这一部分,您将总秒数转换为时间,这就是为什么您将输出作为时间:
SEC_TO_TIME(AVG(TIME_TO_SEC(Response_Time)))
无需将结果转换回时间,只需将秒数除以 60 并四舍五入即可得到总分钟数:
ROUND(AVG(TIME_TO_SEC(Response_Time)) / 60)
在您最初的 SQL 声明中:
SELECT ROUND(AVG(TIME_TO_SEC(Response_Time)) / 60) AS avg_time
FROM Case_report
where Reporting_Time between "'.$fromdate.'" and "'.$todate.'"
and Police_Circle="Sadar"