Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate hour:minutes from total minutes?

Tags:

php

hour

For example i have 525 minutes, if we will divide it by 60 the result will be 8.75

But 1 hour have only 60 minutes not 75

How can i calculate the exact hour:minutes from total minutes?

like image 321
Valoda Avatar asked Oct 28 '11 16:10

Valoda


2 Answers

$hours = intval($totalMinutes/60);
$minutes = $totalMinutes - ($hours * 60);

Edited to be PHP

like image 151
Michael Wildermuth Avatar answered Sep 30 '22 09:09

Michael Wildermuth


This kind of conversion is done using integer division and the modulo operator. With integer division you find out how many of the "large" unit you have and with modulo you find out how many of the "small" unit are left over:

define('MINUTES_PER_HOUR', 60);

$total_minutes = 525;
$hours = intval($total_minutes / MINUTES_PER_HOUR);  // integer division
$mins = $total_minutes % MINUTES_PER_HOUR;           // modulo

printf("%d minutes is really %02d:%02d.\n", $total_minutes, $hours, $mins);

See it in action.

like image 45
Jon Avatar answered Sep 30 '22 08:09

Jon