Convert seconds into days

Asked

Viewed 1,628 times

4

I have a very simple question: How to convert seconds into days? tried using:

<?php echo date('d', $userstats['OnlineTime']); ?>

but only goes up to 31 days.

<?php
   $userstats_a = mysql_query("SELECT * FROM user_stats INNER JOIN users ON user_stats.id = users.id WHERE users.Rank <5 ORDER BY user_stats.OnlineTime DESC LIMIT 5");

   while($userstats = mysql_fetch_assoc($userstats_a)){
   ...

3 answers

4


1

You can do an operation on your own SELECT:

SELECT OnlineTime / 60 / 24 AS Tempo FROM user_stats INNER JOIN users ON user_stats.id = users.id WHERE users.Rank <5 ORDER BY user_stats.OnlineTime DESC LIMIT 5 
  • 1

    You’re turning days into minutes, not seconds into days

  • Hello, it didn’t work....

-1

You can get the expected result using the class Datetime, take the example:

We created a function to facilitate use:

function converterSegundos($segundos, $formato) {
    $dtF = new \DateTime('@0');
    $dtT = new \DateTime("@$segundos");
    return $dtF->diff($dtT)->format($formato);
}

Now to use simply call the function informing a format, below returns in seconds.

// 6 segundos
echo converterSegundos(6, '%s segundos');

Now only minutes:

// 6 minutos
echo converterSegundos(369, '%i minutos');

You can return the time:

// 10 horas
echo converterSegundos(36000, '%h horas');

You can return the days:

// 60 dias
echo converterSegundos(5184000, '%a dias');

Or

// 60 dias, 0 horas, 0 minutos e 0 segundos
echo converterSegundos(5184000, '%a dias, %h horas, %i minutos e %s segundos');

See working on repl it.

Reference

  • Didn’t work either...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.