5

I am using this function to convert seconds to minutes:

function convert($iSeconds)
{
    return date('i:s', mktime(0, 0, $iSeconds));
}

It works great. But if I use this code:

echo convert(3605);

It returns 00:05. But I need to return like this: 60:05

HOW CAN I FORMAT MINUTES:SECONDS???

Aykhan Amiraslanli
  • 593
  • 2
  • 8
  • 22
  • i'm pretty sure if you change the format to `"H:i:s"` its going to be `1:00:05`. i think you have to manually convert hours to minutes by not using `date` functions. for example what if yor given is `7210`? – roullie Jan 27 '16 at 06:15
  • convert(3605); here you are inserting miliseconds not seconds if you try convert(600500); then you can see – Harish Kumar Jan 27 '16 at 06:20

3 Answers3

4

You can't use mktime() or strtotime() for this kind of conevrsions. You can calculate this manually. You can try this -

function convert($iSeconds)
{
   $min = intval($iSeconds / 60);
   return $min . ':' . str_pad(($iSeconds % 60), 2, '0', STR_PAD_LEFT);
}


echo convert(7205);

Output

120:05

Fiddle

Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1

You can't use the date conversion function since it will not accept minutes > 60 and hours > 24.

The easiest way to do it is to use this function:

function convert($iSeconds)
{
    $numMinutes = $iSeconds/60;
    $numSeconds = $iSeconds - ($numMinutes * 60);
    $numMinutes = $numMinutes."";
    $numSeconds = $numSeconds."";
    if (strlen($numMinutes) == 1)
        $numMinutes = "0".$numMinutes;
    if (strlen($numSeconds) == 1)
        $numSeconds = "0".$numSeconds;
    return $numMinutes.":".$numSeconds;
}
itoctopus
  • 4,133
  • 4
  • 32
  • 44
1

According to the documentation of mktime, for minute:

The number of the minute relative to the start of the hour. Negative values reference the minute in the previous hour. Values greater than 59 reference the appropriate minute in the following hour(s).

And for second:

The number of seconds relative to the start of the minute. Negative values reference the second in the previous minute. Values greater than 59 reference the appropriate second in the following minute(s).

When you call mktime you are passing a value of 0 for both hours and minutes; with 3605 seconds you are basically "spinning" the seconds hand on the clock 3600/60 times, and then you add those 5 seconds.

So you will have to first manually calculate the correct values for minutes and possibly hours that your given amount of seconds amounts to.

For that, you will need to use mod and intval of the division.

In a very educational format you'd have something like:

function convert($iSeconds) {
  $seconds = $iSeconds % 60;
  $minutes = intval($iSeconds/60);
  $hours = intval($minutes/60);
  $minutes = $minutes % 60;

  return date('H:i:s', mktime($hours, $minutes, $seconds));
}

https://eval.in/508531

Indeed, this doesn't directly solve the OP, as the number of minutes can be of arbitrary size. As noted, mktime cannot be used to achieve that, as it will always format a "proper" time value, where minutes and seconds are a number between 0-59.

sal
  • 3,515
  • 1
  • 10
  • 21