开发者

How to get total minutes to display as Xhrs, Xmin?

开发者 https://www.devze.com 2023-04-11 10:47 出处:网络
I\'m taking a total number of minutes and am trying to calculate total hrs & minutes. If you do: $elapsed = \"6476\"; // Trying to get 107:56 (107 hours, 56 min)

I'm taking a total number of minutes and am trying to calculate total hrs & minutes.

If you do:

$elapsed = "6476"; // Trying to get 107:56 (107 hours, 56 min)

if ($elapsed > 60) { $format = "i:s"; }
if ($elapsed > 3600) { $format = "H:i:s"; } 

$showdiff = gmdate($format, $elapsed);

The problem with this is it doesn't work for calculations above 23hrs 59min.

So, you can do simple division:

$elapsed开发者_如何学Python = $elapsed / 60; // total hours

Only then you will get a fraction (in this case, 107.933333333). I need to keep this as hours and minutes. Any suggestions?


You should be able to use division (as you've stated) and modulus division to accomplish what you want.

$hours = floor($elapsed / 60);
$minutes = round(($elapsed / 60) % 60);

echo $hours . "hrs " . $minutes . "min";


you could try

$elapsed = $elapsed / 60; // total hours (107.933333333)
$parts = explode(".", $elapsed); // split 107.93333 into 2 parts using the . as a separator
$minutes = $parts[1] // $parts[0] is 107, this will be the .9333
$minutes = $minutes * 60 // = 55.99...
$minutes = round($minutes); // round 55.9999 up to 56
$hours = $parts[0];

echo "$hours hrs $minutes mins";

probably could be written a bit better but my excuse is I'm tired :)

That should give you an idea though and let you do what you need to.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号