0

Possible Duplicates:
Output is in seconds. convert to hh:mm:ss format in php
How can i display seconds ago/minutes ago/with unix timestamp?

I have this users online script I am working on. So far it is as follows...

while ($online_user = $q -> fetch(PDO::FETCH_ASSOC)) {

$last_active = time() - $online_user['online'];
echo '<p>' . $online_user['username'] . ' was last active ' . $last_active . ' seconds
    ago.</p>';

}

$online_user['online'] is the last time() the user was active on the website. So when I minus it from the current time() it gives me the amount of seconds ago that they were last active.

Thing is at the minute it echos like this:

Some username was last active 567 seconds ago.

I would like to know how I can make it echo so it would instead convert it to minutes and seconds so it would look something like this;

Some username was last active 9 minutes and 27 seconds ago.

Thanks in advance if you know of anywhere I could learn this please post a link. :)

Community
  • 1
  • 1
carlgcoder
  • 229
  • 1
  • 3
  • 10
  • Look into php's `date()` – Thom Wiggers Aug 28 '11 at 10:40
  • `date()` isn't very reliable when it comes to time differences. It won't work if the difference is more than 24 hours, or in this case if the difference is more than an hour. – JJJ Aug 28 '11 at 10:42

6 Answers6

6

You can divide by 60 to get the number of minutes. The division remainder is the number of seconds not in a minute.

$minutes = floor($last_active / 60);
$seconds = $last_active % 60;
echo 'Some username was last active ';

if ($minutes > 0) {
    echo "$minutes minutes ";
}

if ($seconds > 0) {
    if ($minutes > 0) {
        echo "and ";
    }
    echo "$seconds seconds ";
}
echo "ago.";

This will break it down into minutes and seconds, and won't output the minutes or seconds if either is zero (ie. no "0 minutes and 30 seconds ago". Note if you ever want to add hours (X hours, Y minutes, Z seconds), you can just take the same approach, dividing again by 60 (60 minutes per hour, 60 seconds per minute). To get 'days', you would divide by 24 (24 hours per day).

JJ.
  • 5,425
  • 3
  • 26
  • 31
5
function sec_to_time($seconds) {
  $hours = floor($seconds / 3600);
  $minutes = floor($seconds % 3600 / 60);
  $seconds = $seconds % 60;

  return sprintf("%d:%02d:%02d", $hours, $minutes, $seconds);
} 
Davide Pastore
  • 8,678
  • 10
  • 39
  • 53
Trefex
  • 2,290
  • 16
  • 18
2

The basic process is the same whatever language you use:

mins = floor (secs / 60);
secs = secs mod 60;

hour = floor (mins / 60);
mins = mins mod 60;
phoxis
  • 60,131
  • 14
  • 81
  • 117
0
$sec = $last_active  % 60;
$min = ($last_active - $sec) / 60;
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
0

You can have the minutes with a division by 60 and the seconds with a modulo :

$minutes = $last_active / 60;
$seconds = $last_actives % 60;
Sylvain Cleymans
  • 1,829
  • 16
  • 13
0
$minutes = $last_active / 60;
$seconds = $last_active % 60;
echo '<p>' . $online_user['username'].' was last active '.$minutes.' minutes and '.$seconds.' seconds ago.</p>';

you can add extra if to check if minutes are 0 not to show the "minutes and " part.