Reputation: 6370
I have a variable called $final_time_saving
which is just a number of minutes, 250 for example.
How can I convert that number of minutes into hours and minutes using PHP in this format:
4 hours 10 minutes
Upvotes: 85
Views: 160857
Reputation: 39389
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
Upvotes: 160
Reputation: 11
2022 answer using Carbon
Carbon::createFromTime(
intdiv($final_time_saving, 60),
($final_time_saving % 60),
0,
0)
->format('H:i')
Upvotes: 0
Reputation: 41
Thanks to @Martin_Bean and @Mihail Velikov answers. I just took their answer snippet and added some modifications to check,
If only Hours only available and minutes value empty, then it will display only hours.
Same if only Minutes only available and hours value empty, then it will display only minutes.
If minutes = 60, then it will display as 1 hour. Same if minute = 1, the output will be 1 minute.
Changes and edits are welcomed. Thanks. Here is the code.
function convertToHoursMins($time) {
$hours = floor($time / 60);
$minutes = ($time % 60);
if($minutes == 0){
if($hours == 1){
$output_format = '%02d hour ';
}else{
$output_format = '%02d hours ';
}
$hoursToMinutes = sprintf($output_format, $hours);
}else if($hours == 0){
if ($minutes < 10) {
$minutes = '0' . $minutes;
}
if($minutes == 1){
$output_format = ' %02d minute ';
}else{
$output_format = ' %02d minutes ';
}
$hoursToMinutes = sprintf($output_format, $minutes);
}else {
if($hours == 1){
$output_format = '%02d hour %02d minutes';
}else{
$output_format = '%02d hours %02d minutes';
}
$hoursToMinutes = sprintf($output_format, $hours, $minutes);
}
return $hoursToMinutes;
}
Upvotes: 1
Reputation: 1906
check this link for better solution. Click here
$minutes=$item['time_diff'];
$hours = sprintf('%02d',intdiv($minutes, 60)) .':'. ( sprintf('%02d',$minutes % 60));
Upvotes: -1
Reputation: 552
$m = 250;
$extraIntH = intval($m/60);
$extraIntHs = ($m/60); // float value
$whole = floor($extraIntHs); // return int value 1
$fraction = $extraIntHs - $whole; // Total - int = . decimal value
$extraIntHss = ($fraction*60);
$TotalHoursAndMinutesString = $extraIntH."h ".$extraIntHss."m";
Upvotes: 1
Reputation: 131
Just in case you want to something like:
echo date('G \h\o\u\r\s i \m\i\n\u\t\e\s', mktime(0, 90)); //will return 1 hours 30 minutes
echo date('G \j\a\m i \m\e\n\i\t', mktime(0, 90)); //will return 1 jam 30 menit
Upvotes: 4
Reputation: 257
$t = 250;
$h = floor($t/60) ? floor($t/60) .' hours' : '';
$m = $t%60 ? $t%60 .' minutes' : '';
echo $h && $m ? $h.' and '.$m : $h.$m;
4 hours and 10 minutes
Upvotes: 8
Reputation: 302
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(250); //Function Call will return value : 04 Hours 10 Minutes
Upvotes: 1
Reputation: 43552
You can achieve this with DateTime
extension, which will also work for number of minutes that is larger than one day (>= 1440
):
$minutes = 250;
$zero = new DateTime('@0');
$offset = new DateTime('@' . $minutes * 60);
$diff = $zero->diff($offset);
echo $diff->format('%a Days, %h Hours, %i Minutes');
Upvotes: 13
Reputation: 51
Sorry for bringing up an old topic, but I used some code from one of these answers a lot, and today I told myself I could do it without stealing someone's code. I was surprised how easy it was. What I wanted is 510 minutes to be return as 08:30, so this is what the code does.
function tm($nm, $lZ = true){ //tm = to military (time), lZ = leading zero (if true it returns 510 as 08:30, if false 8:30
$mins = $nm % 60;
if($mins == 0) $mins = "0$mins"; //adds a zero, so it doesn't return 08:0, but 08:00
$hour = floor($nm / 60);
if($lZ){
if($hour < 10) return "0$hour:$mins";
}
return "$hour:$mins";
}
I use short variable names because I'm going to use the function a lot, and I'm lazy.
Upvotes: 5
Reputation: 566
@Martin Bean's answer is perfectly correct but in my point of view it needs some refactoring to fit what a regular user would expect from a website (web system).
I think that when minutes are below 10 a leading zero must be added.
ex: 10:01, not 10:1
I changed code to accept $time = 0
since 0:00 is better than 24:00.
One more thing - there is no case when $time
is bigger than 1439 - which is 23:59 and next value is simply 0:00.
function convertToHoursMins($time, $format = '%d:%s') {
settype($time, 'integer');
if ($time < 0 || $time >= 1440) {
return;
}
$hours = floor($time/60);
$minutes = $time%60;
if ($minutes < 10) {
$minutes = '0' . $minutes;
}
return sprintf($format, $hours, $minutes);
}
Upvotes: 9
Reputation: 75568
$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;
Upvotes: 50