我正在运行以下查询
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分钟之内就得出结果
答案 0 :(得分:2)
在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"
答案 1 :(得分:0)
您可以在PHP中使用一个简单的函数,例如:
function isValidDate(str) {
var d = moment(str,moment.ISO_8601);
if (d == null || !d.isValid())
{
return false;
}
match = ['YYYY-MM-DD','D/M/YYYY','DD/MM/YYYY','D/M/YY','DD/MM/YY','D-M-YYYY','DD-MM-YYYY','D-M-YY','DD-MM-YY','YYYY-MM-DD','YYYY-DD-MM',"MMMM D,YYYY","MMM Do,YYYY"] ;
for(let i=0;i<12;i++)
{
if(str.indexOf(d.format(match[i])) >= 0 )
{
return match[i];
}
}
}
输出:
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);