-1

I am using Google Maps API to get the duration in between two points. That API gives the duration in hours and mins. So I want it to convert only to minutes using PHP code. Are there any solutions?

Ex: 2 hours 10 mins (Should give 130 mins as the output)

Maxime
  • 838
  • 6
  • 18

2 Answers2

2

Try something like this

    // Transform hours like "1:45" into the total number of minutes, "105". 
function hoursToMinutes($hours) 
{ 
    $minutes = 0; 
    if (strpos($hours, ':') !== false) 
    { 
        // Split hours and minutes. 
        list($hours, $minutes) = explode(':', $hours); 
    } 
    return $hours * 60 + $minutes; 
} 

// Transform minutes like "105" into hours like "1:45". 
function minutesToHours($minutes) 
{ 
    $hours = (int)($minutes / 60); 
    $minutes -= $hours * 60; 
    return sprintf("%d:%02.0f", $hours, $minutes); 
}  

Src : laserlight -> http://board.phpbuilder.com/showthread.php?10342598-quot-Convert-quot-hours-minutes-quot-to-quot-total_minutes-quot-and-back-quot

Maxime
  • 838
  • 6
  • 18
  • 2
    please credit the author, http://board.phpbuilder.com/showthread.php?10342598-quot-Convert-quot-hours-minutes-quot-to-quot-total_minutes-quot-and-back-quot – Tirolel Sep 22 '17 at 06:59
0

Thanks for the help guys, I found another solution...This works for me

$duration = "What Ever the time (2 hours 10 mins)"

$split = preg_split("/[^\w]*([\s]+[^\w]*|$)/", $duration, -1, PREG_SPLIT_NO_EMPTY);

    if ($split[0] >= 1 && $split[0] < 60 && $split[1] == 'min'){
        $duration = $split[0];
    }
    elseif ($split[0] >= 1 && $split[0] < 60 && $split[1] == 'mins'){
        $duration = $split[0];
    }
    else{
        $durationHours = $split[0]*60;
        $durationMin = $split[2];

        $duration = $durationHours + $durationMin;
        echo $duration;
    }
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58