PHP: Convert hour fractions to hour:minutes

Using PHP, how can I convert hours with fractions to hours with minutes?

For example,
echo hourMinutes(4.5); // 4:30
echo hourMinutes(-4.5); // -4:30
echo hourMinutes(1.9); // 1:54
echo hourMinutes(22); // 22:00

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
try:
function hourMinutes($t) {
	return (($t < 0)?"-":"").date("G:i", mktime(0,abs($t)*60));
}

echo hourMinutes(4.5)."<br>";
echo hourMinutes(-4.5)."<br>";
echo hourMinutes(1.9)."<br>";
echo hourMinutes(22)."<br>";

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brian TaoSenior Business Solutions ConsultantCommented:
function hourMinutes($inVal)
{
  if (!is_numeric($inVal)){
    // you may throw an exception if the input is not a number
    // throw new Exception("Not a number.");
    
    // or just return a "standard" value, e.g. false, that tells the caller it's not valid
    return false;
  }

  $sign = ($inVal < 0 ? "-" : "");
  $absMinutes = abs($inVal) * 60;
  
  $hh = floor($absMinutes / 60);
  $mm = $absMinutes % 60;
  return $sign . $hh . ":" . str_pad($mm, 2, "0", STR_PAD_LEFT);
}

echo "Test Result:";
echo "<br>" . hourMinutes(124.3); // 124:18
echo "<br>" . hourMinutes(-124.3); // -124:18
echo "<br>" . hourMinutes(4.5); // 4:30
echo "<br>" . hourMinutes(-4.5); // -4:30
echo "<br>" . hourMinutes(1.9); // 1:54
echo "<br>" . hourMinutes(22); // 22:00

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
I formatted the hours to days if it exceeds 24 hours:
function hourMinutes($t) {
	return (($t < 0)?"-":"").date((abs($t)>24)?floor(abs($t)/24).":H:i":"G:i", mktime(0,abs($t)*60));
}

echo hourMinutes(4.5)."<br>";
echo hourMinutes(-4.5)."<br>";
echo hourMinutes(1.9)."<br>";
echo hourMinutes(22)."<br>";
echo hourMinutes(124.3)."<br>";
echo hourMinutes(-124.3)."<br>";

Open in new window

Ray PaseurCommented:
This works for in-range values.  I haven't tested it for out-of-range values.
http://iconoun.com/demo/temp_skij.php
<?php // demo/temp_skij.php

/**
 * http://www.experts-exchange.com/questions/28734719/PHP-Convert-hour-fractions-to-hour-minutes.html
 * http://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html
 */
error_reporting(E_ALL);
echo '<pre>';

$hourFractions =
[ 4.5
, -4.5
, 1.9
, 22
, 1.05
]
;
foreach ($hourFractions as $h)
{
    echo PHP_EOL . "$h => " . hourminutes($h);
}


function hourMinutes($numbr)
{
    $hours = floor($numbr);
    if ($numbr < 0.0) $hours = ceil($numbr);
    $hours = (int) $hours;

    $minutes = explode('.', $numbr);
    if (empty($minutes[1])) $minutes[1] = '0';
    $minutes = trim($minutes[1]);
    $minutes = ( 60 * ('0.' . $minutes) );
    $minutes = number_format($minutes,0);
    $minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT);
    return $hours . ':' . $minutes;
}

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.