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. 🙂
You can divide by 60 to get the number of minutes. The division remainder is the number of seconds not in a minute.
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).