I know that when you read the title you said “What is he talking about? Its easy”. Well it turns out that its not that easy. It took me a while to collect and create this, but here it is: a PHP code that converts minutes to hours.
<?php function ConvertMinutes2Hours($Minutes)
{
if ($Minutes < 0)
{
$Min = Abs($Minutes);
}
else
{
$Min = $Minutes;
}
$iHours = Floor($Min / 60);
$Minutes = ($Min - ($iHours * 60)) / 100;
$tHours = $iHours + $Minutes;
if ($Minutes < 0)
{
$tHours = $tHours * (-1);
}
$aHours = explode(".", $tHours);
$iHours = $aHours[0];
if (empty($aHours[1]))
{
$aHours[1] = "00";
}
$Minutes = $aHours[1];
if (strlen($Minutes) < 2)
{
$Minutes = $Minutes ."0";
}
$tHours = $iHours .":". $Minutes;
return $tHours;
}
//Test Code
echo ConvertMinutes2Hours(220); // This should print 3:40
?>
Hope this helps someone
Amit.
We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution
Learn More
André Medeiros Said on Mar 18, 2008 :
You can do it in far less lines.
function convertMinutes2Hours($minutes) {
$minutes = round($minutes); // assume full minutes
$hours = floor($minutes / 60);
$minutes = $minutes - ($hours * 60);
return str_repeat(’0′, 2 - strlen($hours)) . $hours . ‘:’ . str_repeat(’0′, 2 - strlen($minutes)) . $minutes;
}
André Medeiros Said on Mar 19, 2008 :
One-liner (if you’re not planning on using more than 24 hours):
function convertMinutes2Hours($minutes) { return date(”G:i”, abs(round((int)$minutes))*60); }
Evan Wired Said on Mar 26, 2008 :
If $min is an integer, it is even simpler:
$hours = sprintf(”%d:%02d”, abs((int)($min/60)), abs((int)($min%60)));
The original code is a DailyWTF candidate.
Admin Said on Mar 26, 2008 :
Thanks for all the comments.
I like your ideas and i will definitely use them
Evan Wired Said on Mar 26, 2008 :
Don’t use what I posted. Got too silly with the parens. Lesson: Always take more than 10 seconds to write your code
$hours = sprintf(”%d:%02d”, abs((int)$min/60), abs((int)$min%60));
stratos Said on Jun 2, 2008 :
that str_repeat function is nice
but i think date is the most appropriate function i guess…
Dennis Said on Jul 5, 2008 :
Sweet, thanks all. I’ve converted this to a tad longer, but more usable, returned text.
function ConvertMinutes2Hours($Min) {
$iHours = Floor($Min / 60);
$Minutes = ($Min - ($iHours * 60)) / 100;
$tHours = $iHours + $Minutes;
if ($Minutes < 0){
$tHours = $tHours * (-1);
}
$aHours = explode(”.”, $tHours);
$iHours = $aHours[0];
if (empty($aHours[1])) {
$aHours[1] = “00″;
}
$Minutes = $aHours[1];
if (strlen($Minutes) < 2) {
$Minutes = $Minutes .”0″;
}
if ($iHours==0) {$tHours = $Minutes.’ minutes’;}
else if ($iHours==1) {$tHours = $iHours.” hour and “.$Minutes.’ minutes’;}
else {$tHours = $iHours.” hours and “.$Minutes.’ minutes’;}
return $tHours;
}
Warz Said on Oct 11, 2008 :
mmh you could use mktime
$sec = 500;
date(’H:i:s’,mktime(0,0,$sec,0,0,0));
André Medeiros Said on Oct 14, 2008 :
@Wars
Creative use of the mktime function. Right on!
Luke H Said on Apr 6, 2009 :
Warz. that one line use of mktime to do this is just gorgeous. Thanks!
Francis Said on Aug 2, 2009 :
I’ve changed the initial function so it displays days too.
function ActiveMinutesToHours($Minutes)
{
if ($Minutes < 0)
{
$Min = Abs($Minutes);
}
else
{
$Min = $Minutes;
}
$iHours = Floor($Min / 60);
$Minutes = ($Min - ($iHours * 60)) / 100;
$tHours = $iHours + $Minutes;
if ($Minutes < 0)
{
$tHours = $tHours * (-1);
}
$aHours = explode(”.”, $tHours);
$iHours = $aHours[0];
if (empty($aHours[1]))
{
$aHours[1] = “00″;
}
$Minutes = $aHours[1];
if (strlen($Minutes) 24)
{
$iDays = $iHours/24;
if($iDays == 1)
$iDays .= ” day, “;
else
$iDays .= ” days, “;
}
$tHours = $iDays . $iHours .” hours and “. $Minutes . “minutes.”;
return $tHours;
}