11

I would like to display time in minues as an hour and minutes.

Example 1: I want to display 125 minutes as a 2:05

I know I can to somethink like:

$minutes=125;
$converted_time = date('H:i', mktime(0,$minutes);

This works fine, but if the time is more then 24h it is a problem.

Example 2:

$minutes=1510;

and I want to receive 25:10 (without days), only hours and minutes.

How to do that?

Tikky
  • 1,253
  • 2
  • 17
  • 36
  • `date` functions will not help you, because you're not working with dates! This is a trivial manual operation, mostly involving a `/60` operation. – deceze Jul 16 '14 at 09:54
  • 1
    please try to do a brief search before posting new questions .. – Qarib Haider Jul 16 '14 at 09:56

3 Answers3

47

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);
Mohd Abdul Mujib
  • 13,071
  • 8
  • 64
  • 88
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • $hours = floor($minutes / 60).':'.($minutes % 60); – T'lash Mar 16 '17 at 16:55
  • but here something is missing. your answer is correct but not time format .my means was your answer come like 8:60 or 9:01 or 9:10. here hour value is single value. it will be better if it comes in form 09:23 like this way.. i posted my solution here you can check. – pankaj Jan 17 '20 at 07:53
  • 2
    check my solution, $minutes=$item['time_diff']; $hours = sprintf('%02d',intdiv($minutes, 60)) .':'. ( sprintf('%02d',$minutes % 60)); – pankaj Jan 17 '20 at 07:55
  • When the minutes are in single digits, the solution provided by this answer does not make sense. Ex: For 67 minutes, it returns: `1:7`. Instead it should be `1:07`. Please include formatting with `sprintf` to correct the answer. – Raghavendra N Mar 31 '20 at 11:22
7

As simple as that.

$minutes = 125;

$hours = floor($minutes / 60);
$min = $minutes - ($hours * 60);

echo $hours.":".$min;

EDIT: should use floor() instead of round() for getting correct results.

JayKandari
  • 1,228
  • 4
  • 16
  • 33
5
$hours = floor($minutes / 60); // Get the number of whole hours
$minutes = $minutes % 60; // Get the remainder of the hours

printf ("%d:%02d", $hours, $minutes); // Format it as hours:minutes where minutes is 2 digits
scragar
  • 6,764
  • 28
  • 36