1

I have a no. of minute. how to convert minutes into hours.I use following code but it is not working properly.

$duration  = "100";
$duration = strtotime($duration." minutes");
echo $duration = date('H:i:s', $duration);

expected output: 01:40:00

but it gives this output:15:10:02

Thanks in advance.

DS9
  • 2,995
  • 4
  • 52
  • 102

2 Answers2

1

The problem is that you try to print out a duration as a date. Try this instead:

$hours = floor($duration / 60);
$mins = $duration % 60;

echo str_pad($hours, 2, '0', STR_PAD_LEFT) , ':' , str_pad($mins, 2, '0', STR_PAD_LEFT) , ':00';
TiMESPLiNTER
  • 5,741
  • 2
  • 28
  • 64
0

Your not giving strtotime a full date so it is using your current time as the reference point. If you were to wait a minute and try again it would give you a different result. If for some reason you are set on using strtotime then try this:

$duration  = "100";
$duration = strtotime("2000-01-01 00:00:00 +{$duration} minutes");
echo $duration = date('H:i:s', $duration);

This is not how I would recommend converting minutes to hours, days, etc... though.

Pitchinnate
  • 7,517
  • 1
  • 20
  • 37