How to get H:i:s from minutes?

239 Views Asked by At

I have a value 15 min from which i need to get H:i:s. Tried date('H:i:s', strtotime('15 min')) but it returns 15:23:47 and i need 00:15:00.

3

There are 3 best solutions below

0
On

How about:

<?php

    $time = "15 min";
    echo date('H:i:s', strtotime("midnight + " . $time));

?>
0
On

You have many solutions for getting the H:i:s from minutes.


<?php
echo date('H:i:s', strtotime("midnight + 15 min"));

?>


<?php
echo date('H:i:s', strtotime("today + 15 min"));

?>


<?php
echo date('H:i:s', strtotime("tomorrow + 15 min"));

?>

0
On

Your value of '15 mins', in this case is not a time, it is a time interval and is most appropriately represented by the DateInterval class. Using the right tool makes the job very simple:-

$interval = \DateInterval::createFromDateString('15 min');
echo $interval->format('%H:%I:%S');

Output:-

00:15:00

You could get the same output in one line if you are a fan of that kind of thing:-

echo \DateInterval::createFromDateString('15 min')->format('%H:%I:%S');

However the first option has the advantage of keeping the DateInterval instance available for further use.