Convert minutes to time 'h:m:s'

1.8k views Asked by At

I wonder if there is a function in php or codeigniter which can do this :

function transformTime($min)
{
    $min=(int)$min;
    $heure=(int)($min/60);
    $minute=(($min/60)-$heure)*60;


    return $heure .':' . $minute . ':00'; 
}

I want convert x minutes to a time format.

2

There are 2 answers

2
Shankar Narayana Damodaran On

Do something like this

<?php

function transformTime($min)
{
$ctime = DateTime::createFromFormat('i', $min);
$ntime= $ctime->format('H:i:s');
return $ntime;
}

echo transformTime(60); // "Prints" 01:00:00
0
Glavić On

You are almost there, just format the output with sprintf() or str_pad():

function transformTime($min)
{
    $hour = floor($min / 60);
    $min -= $hour * 60;
    return sprintf('%02d:%02d:00', $hour, $min);
}