我需要找到平均时间。学生考试的时间。我有开始时间和结束时间。和总数 但它给出了错误。
$start_dateav = new DateTime($start_time_av);
致命错误:带有消息的未捕获异常'异常' 'DateTime :: __ construct():无法解析时间字符串(2015年12月19日 01:55:13 pm)位置0(1):意外字符'in E:\ xampp \ htdocs \ mock \ report.php:117堆栈跟踪:#0 E:\ XAMPP \ htdocs中\模拟\ report.php(117): DateTime-> __ construct('19 / 12/2015 01:5 ...')#1 {main}引入 第117行的E:\ xampp \ htdocs \ mock \ report.php
function average_time($total, $count, $rounding = 0) {
$total = explode(":", strval($total));
if (count($total) !== 3) return false;
$sum = $total[0]*60*60 + $total[1]*60 + $total[2];
$average = $sum/(float)$count;
$hours = floor($average/3600);
$minutes = floor(fmod($average,3600)/60);
$seconds = number_format(fmod(fmod($average,3600),60),(int)$rounding);
return $hours.":".$minutes.":".$seconds;
}
$sqlav="select * from user_test where test_id = '$test_id'";
$resultav=mysqli_query($con,$sqlav);
$takentimeavH = "0";
$takentimeavM = "0";
$takentimeavS = "0";
$totaltst = "3237";
while ($totaltstav=mysqli_fetch_array($resultav)) {
$start_time_av=date('d/m/Y h:i:s a',strtotime($totaltstav['start_time']));
$end_time_av=date('d/m/Y h:i:s a',strtotime($totaltstav['end_time']));
$start_dateav = new DateTime($start_time_av);
$since_startav = $start_dateav->diff(new DateTime($end_time_av));
$takentimeavH += $since_startav->h;
$takentimeavM += $since_startav->i;
$takentimeavS += $since_startav->s;
}
$tataltime = $takentimeavH.':'.$takentimeavM.':'.$takentimeavS;
echo average_time($tataltime, $totaltst);
在同一个php页面上,我使用相同的方法来获取时间。工作正常。
$sqlm="select * from user_test where test_id = '$test_id' order by mark desc limit 1";
$resultm=mysqli_query($con,$sqlm);
$totaltstm=mysqli_fetch_array($resultm);
$maxmarkss=$totaltstm['mark'];
$start_time_toppr=date('d/m/Y h:i:s a',strtotime($totaltstm['start_time']));
$end_time_toppr=date('d/m/Y h:i:s a',strtotime($totaltstm['end_time']));
$start_datetppr = new DateTime($start_time_toppr);
$since_starttppr = $start_datetppr->diff(new DateTime($end_time_toppr));
$takentimetppr = $since_starttppr->h.':'.$since_starttppr->i.':'.$since_starttppr->s.'';
答案 0 :(得分:0)
解决了我使用日期 Y-m-d 而不是 d / m / Y