I have a time column $data['Time'] (hh:mm:ss)
and I need to convert it to minutes. How can I do this? When I am writing like this:
$avg = ($data['Kilometers'] / $data['Time']) * 60;
I have this error
Warning: Division by zero in ... on line ..
To convert hh:mm:ss to seconds:Convert the hours to seconds, by multiplying by 60 twice. Convert the minutes to seconds by multiplying by 60 .
Try sometthin like this :
function minutes($time){
$time = explode(':', $time);
return ($time[0]*60) + ($time[1]) + ($time[2]/60);
}
LIVE DEMO
$time = explode(':', $data['Time']);
$minutes = ($time[0] * 60.0 + $time[1] * 1.0);
$avg = $minutes > 0 ? $data['Kilometers'] / $minutes : 'inf'; // if time stored is 0, then average is infinite.
Another way to convert the timestamp to minutes is,
$time = date('i', strtotime($data['Time']));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With