Convert Number of Minutes into Hours & Minutes Using PHP

How to convert minutes to hours and minutes (without days)

You can use:

$minutes=1510;

$hours = intdiv($minutes, 60).':'. ($minutes % 60);

!!! This only works with php >= v7.xx

Previous answer:

$minutes=1510;

$hours = floor($minutes / 60).':'.($minutes - floor($minutes / 60) * 60);

Convert number of minutes into hours & minutes using PHP

<?php

function convertToHoursMins($time, $format = '%02d:%02d') {
if ($time < 1) {
return;
}
$hours = floor($time / 60);
$minutes = ($time % 60);
return sprintf($format, $hours, $minutes);
}

echo convertToHoursMins(250, '%02d hours %02d minutes'); // should output 4 hours 17 minutes

how to convert minutes into hours:minutes:seconds?

The problem is that you try to print out a duration as a date. Try this instead:

$hours = floor($duration / 60);
$mins = $duration % 60;

echo str_pad($hours, 2, '0', STR_PAD_LEFT) , ':' , str_pad($mins, 2, '0', STR_PAD_LEFT) , ':00';

Convert seconds to Hour:Minute:Second

You can use the gmdate() function:

echo gmdate("H:i:s", 685);

Convert minutes into Hours and Minutes

Both the date and gmdate function get seconds as input not minutes. So all you have to do is multiply your startminutes by 60 to get your answer to work.

$startT = $currDate . " " . gmdate("H:i", ($startMinutes * 60));

PHP Minutes to hours and minutes

Try this one

function hour_min($minutes){// Total
if($minutes <= 0) return '00 Hours 00 Minutes';
else
return sprintf("%02d",floor($minutes / 60)).' Hours '.sprintf("%02d",str_pad(($minutes % 60), 2, "0", STR_PAD_LEFT)). " Minutes";
}
echo hour_min(500);

this will return output : 08:20

how to convert minutes into hours, hours into days

Try this function,

& Modify it with your requirements

    function con_min_days($mins)
{

$hours = str_pad(floor($mins /60),2,"0",STR_PAD_LEFT);
$mins = str_pad($mins %60,2,"0",STR_PAD_LEFT);

if((int)$hours > 24){
$days = str_pad(floor($hours /24),2,"0",STR_PAD_LEFT);
$hours = str_pad($hours %24,2,"0",STR_PAD_LEFT);
}
if(isset($days)) { $days = $days." Day[s] ";}

return $days.$hours." Hour[s] ".$mins." Min[s]";
}

How to convert a number into hour and minute using php?

Simply try like this

echo floor($t_hr/60)." hr ".($t_hr%60)." min";

Assuming that $t_hr is the minutes

Convert minutes to Hours:Minutes in Oracle SQL

It looks like createdttm is a timestamp, so you can just subtract:

systimestamp - createdtm

... to get an interval value like +000000000 02:00:00.00000. You can't format that directly, but you can either extract the various elements and concatenate those back together, or treat it as a string and cut out the bits you want.

If you only want the time part and it will always be less than a day you can just do:

substr(systimestamp - createdtm, 12, 5)

02:00

But if it can go over 24 hours then you probably want the day part too, which you could still get just with substr (and maybe replace to change the space to another colon) if you know it can never be more than 2 days:

substr(systimestamp - createdtm, 10, 7)

0 02:00

That's unlikely to be a safe assumption though, so instead you could extract the number of days and concatenate that:

extract(day from (systimestamp - createdtm)) || ':' || substr(systimestamp - createdtm, 12, 5)

0:02:00

You could only show the number of days if it's non-zero, but that would probably be quite confusing to who/whatever is looking at the results; but if you really wanted to:

case when extract(day from (systimestamp - createdtm)) > 0
then extract(day from (systimestamp - createdtm)) || ':'
end || substr(systimestamp - createdtm, 12, 5)

02:00

db<>fiddle with a few sample values.

One thing to note is this effectively truncates the seconds off the time; your original attempt included round(), but that might not have been what you meant.



Related Topics



Leave a reply



Submit