devendra devendra - 23 days ago 7
MySQL Question

Find Average Time

I need to find the average time. of students exam taken time. i have start time and end time. and total count.

but its gives error on line.

$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);


on the same php page i am using the same method to get time. its working fine.

$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.'';

Answer

Solved i use the Date Y-m-d instead of d/m/Y

Comments