6

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 ..
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
eek
  • 93
  • 1
  • 1
  • 6

7 Answers7

15

Try sometthin like this :

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

LIVE DEMO

SpencerX
  • 5,453
  • 1
  • 14
  • 21
8
$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']));
Fallen
  • 4,435
  • 2
  • 26
  • 46
  • Thank you! How can I edit the number of digits after the decimal point? – eek Jul 27 '14 at 01:21
  • If I write like this $numberFormat = number_format($avs, 2, '.', ''); I have instead of - 'inf' I have 0.00 – eek Jul 27 '14 at 01:35
  • you can write like, `if($avg != 'inf') numberFormat = number_format($avs, 2, '.', '');` – Fallen Jul 27 '14 at 06:10
  • Why it calculates without seconds? – eek Jul 27 '14 at 11:18
  • @eek: in a valid timestamp, we can assume second will be less than 60. So I didn't added that. But the second approach already handles that – Fallen Jul 27 '14 at 11:25
  • You mean this approach $time = date('i', strtotime($data['Time'])); ? – eek Jul 27 '14 at 16:37
  • $minutes = ($time[0] * 60.0 + $time[1] * 1.0); As I understand. First $time is a hours, second $time is a minutes and I can add $time[2] as a seconds? – eek Jul 27 '14 at 18:18
  • yes `$time[2]` represents number of seconds. If you need to add that, please add like, `$time[2]/60` to ensure you convert that to minute – Fallen Jul 27 '14 at 18:19
  • Thanks a lot. I make like this - $minutes = ($timeAvs[0] * 60.0 + $timeAvs[1] + ($timeAvs[2] / 60) * 1.0); – eek Jul 27 '14 at 18:30
  • 1
    date('i', strtotime('01:30')) == 30 – digout Feb 17 '21 at 09:36
2

This is simple calculation use this one

    $time='01:02:10';
    $timesplit=explode(':',$time);
    $min=($timesplit[0]*60)+($timesplit[1])+($timesplit[2]>30?1:0);
    echo $min.' Min'  // 62 Min
1
You can use following function 

<?php

function date2min ($hms) {

    $fromTime = strtotime($hms);

    $getMins = round(abs($fromTime) / 60,2);

    return $getMins;
}


$date   = date('H:i:s');  // $data['Time'] your desired time
$myResult = date2min($date);

and then use $myResult value according to your need.

?>
Dinesh Nagar
  • 768
  • 2
  • 11
  • 23
1

If hh:mm:ss is a string you can try this which is tested and it works:

 $hour=substr($string, 0,1);// we get the first two values from the hh:mm:ss string
 $hour=(int)$hour;
 $hourtomin=$hour*60;// after we have the hour we multiply by 60 to get the min
 $min=substr($string, 3,4);//now we do a substring 3 to 4 because we want to get only the min, and we don't want to get the : which is in position 2
 $min=(int)$min;
    
 $totalmin=$hourtomin+$min;// we just do a simple sum to calculate all the min
 echo $totalmin;
Alban Kaperi
  • 597
  • 4
  • 12
0
function hourMinute2Minutes($strHourMinute) {
    $from = date('Y-m-d 00:00:00');
    $to = date('Y-m-d '.$strHourMinute.':00');
    $diff = strtotime($to) - strtotime($from);
    $minutes = $diff / 60;
    return (int) $minutes;
}

echo hourMinute2Minutes('01:30'); // returns 90
Erlang Parasu
  • 183
  • 2
  • 3
  • 8
  • 1
    Thanks for contributing with StackOverflow! While showing the solution as code really helps, it would be more helpful if you can include some explanation on how it works and what is the cause of the original problem. – Victor Schröder Oct 23 '19 at 18:14
0

you can do this very simple way.

$minutes=$item['time_diff'];
$hours =   sprintf('%02d',intdiv($minutes, 60)) .':'. ( sprintf('%02d',$minutes % 60));
return $hours;

Output will 09:10

pankaj
  • 1
  • 17
  • 36