2016-11-08 179 views
-1

我需要找到平均时间。的学生考试需要时间。我有开始时间和结束时间。和总数。
但它给出了错误在线。查找平均时间

$start_dateav = new DateTime($start_time_av); 

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (19/12/2015 01:55:13 pm) at position 0 (1): Unexpected character' in E:\xampp\htdocs\mock\report.php:117 Stack trace: #0 E:\xampp\htdocs\mock\report.php(117): DateTime->__construct('19/12/2015 01:5...') #1 {main} thrown in E:\xampp\htdocs\mock\report.php on line 117

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

为什么不这样做在SQL? – Strawberry

回答

0

解决我使用日期Y-M-d代替d/M/Y