Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert hh:mm:ss to minutes

Tags:

php

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 ..
like image 691
eek Avatar asked Jul 26 '14 21:07

eek


People also ask

How do you convert HH mm SS to seconds?

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 .


2 Answers

Try sometthin like this :

function minutes($time){
$time = explode(':', $time);
return ($time[0]*60) + ($time[1]) + ($time[2]/60);
}

LIVE DEMO

like image 162
SpencerX Avatar answered Oct 17 '22 00:10

SpencerX


$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']));
like image 30
Fallen Avatar answered Oct 17 '22 01:10

Fallen