Newbie question about converting minutes to hours

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someonehere
Forum Newbie
Posts: 1
Joined: Thu Jun 10, 2010 3:35 pm

Newbie question about converting minutes to hours

Post by someonehere »

Hello
Please, complete newbie question, but I have a page that takes the information in minutes from MySQL and displays it in hours. The problem is that I'd like for it not to display either the hours or the minutes when they equal 0, for example, currently, when something is 120 minutes, it displays 2h 0m , and when it's 45 minutes it displays 0h 45m. Is there any way those could display simply as 2h or as 45m? The code I am using is below:

if ($row["runtimem"]<>""){
$runh = floor($row["runtimem"] / 60);
$runm = $row["runtimem"] % 60;
echo $runh . "h " . $runm . "m&nbsp;&nbsp;&nbsp;";
}


Please, any ideas would be very appreciated.
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Newbie question about converting minutes to hours

Post by cpetercarter »

Code: Select all

if ($row["runtimem"]<>""){
       $runh = floor($row["runtimem"] / 60);
       $runm = $row["runtimem"] % 60;
       $run = "";
       if ($runh > 0) $run .= $runh . "h ";
       if ($runm > 0) $run .= $runm . "m";
       $run = trim($run) . "&nbsp;&nbsp;&nbsp;"; 
       echo $run;
} 
Post Reply