Convert number of minutes into hours & minutes using PHP

PhpTime

Php Problem Overview


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

Php Solutions


Solution 1 - 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

Solution 2 - Php

echo date('H:i', mktime(0,257));

Solution 3 - Php

$hours = floor($final_time_saving / 60);
$minutes = $final_time_saving % 60;

Solution 4 - Php

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');

demo

Solution 5 - Php

@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);
}

Solution 6 - Php

$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

Solution 7 - Php

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.

Solution 8 - Php

The easiest way is :

    gmdate('H:i', $numberOfSeconds * 60)

Solution 9 - Php

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

Solution 10 - Php

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

Solution 11 - Php

$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";
	

Solution 12 - Php

Thanks to @Martin_Bean and @Mihail Velikov answers. I just took their answer snippet and added some modifications to check,

  1. If only Hours only available and minutes value empty, then it will display only hours.

  2. Same if only Minutes only available and hours value empty, then it will display only minutes.

  3. 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;
		}

Solution 13 - Php

> check this link for better solution. Click here > > https://stackoverflow.com/questions/24975664/how-to-convert-hhmmss-to-minutes/59783258#59783258

$minutes=$item['time_diff'];
$hours =   sprintf('%02d',intdiv($minutes, 60)) .':'. ( sprintf('%02d',$minutes % 60));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRobView Question on Stackoverflow
Solution 1 - PhpMartin BeanView Answer on Stackoverflow
Solution 2 - PhpAlastairView Answer on Stackoverflow
Solution 3 - PhpSjoerdView Answer on Stackoverflow
Solution 4 - PhpGlavićView Answer on Stackoverflow
Solution 5 - PhpMihail VelikovView Answer on Stackoverflow
Solution 6 - PhpMikeView Answer on Stackoverflow
Solution 7 - PhpLurvikView Answer on Stackoverflow
Solution 8 - PhpSeif.benView Answer on Stackoverflow
Solution 9 - PhpkelaskakapView Answer on Stackoverflow
Solution 10 - PhpHIRView Answer on Stackoverflow
Solution 11 - PhpOmprakash PatelView Answer on Stackoverflow
Solution 12 - PhpRamsView Answer on Stackoverflow
Solution 13 - Phppankaj kumarView Answer on Stackoverflow