Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7610693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:28:46+00:00 2026-05-31T01:28:46+00:00

I wanted to know a good and efficient way to be able to tell

  • 0

I wanted to know a good and efficient way to be able to tell how long ago my users last logged in.

On the users profile I want it for say how long ago their last log in was.

Eg:

User 1
Last login: 2 hours ago

User 2
Last login: 3 minutes ago

User 3
Last login: 2 months ago


I will keep their last login information in a MySQL database but want to know how to do the script.

I just realized that Stackoverflow uses this feature, so that can help you understand what I want.


mysql_query("UPDATE users SET lastactivity = ".time()." WHERE id = ".$userID);

This is how I will update the DB.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:28:47+00:00Added an answer on May 31, 2026 at 1:28 am

    Each page request just update their last activity.

    <?php
    
    mysql_query("UPDATE users SET lastactivity = ".time()." WHERE id = ".$userID);
    

    to show when they were last online, just select their lastactivity field from database and show it

    <?php
    $activity = mysql_result(mysql_query("SELECT lastactivity FROM users WHERE id = ".$userID), 0);
    echo "Last activity: ".relativeTime($active);
    

    where relativeTime() is function I’ve been using:

    function relativeTime($time, $short = false){
        $SECOND = 1;
        $MINUTE = 60 * $SECOND;
        $HOUR = 60 * $MINUTE;
        $DAY = 24 * $HOUR;
        $MONTH = 30 * $DAY;
        $before = time() - $time;
    
        if ($before < 0)
        {
            return "not yet";
        }
    
        if ($short){
            if ($before < 1 * $MINUTE)
            {
                return ($before <5) ? "just now" : $before . " ago";
            }
    
            if ($before < 2 * $MINUTE)
            {
                return "1m ago";
            }
    
            if ($before < 45 * $MINUTE)
            {
                return floor($before / 60) . "m ago";
            }
    
            if ($before < 90 * $MINUTE)
            {
                return "1h ago";
            }
    
            if ($before < 24 * $HOUR)
            {
    
                return floor($before / 60 / 60). "h ago";
            }
    
            if ($before < 48 * $HOUR)
            {
                return "1d ago";
            }
    
            if ($before < 30 * $DAY)
            {
                return floor($before / 60 / 60 / 24) . "d ago";
            }
    
    
            if ($before < 12 * $MONTH)
            {
                $months = floor($before / 60 / 60 / 24 / 30);
                return $months <= 1 ? "1mo ago" : $months . "mo ago";
            }
            else
            {
                $years = floor  ($before / 60 / 60 / 24 / 30 / 12);
                return $years <= 1 ? "1y ago" : $years."y ago";
            }
        }
    
        if ($before < 1 * $MINUTE)
        {
            return ($before <= 1) ? "just now" : $before . " seconds ago";
        }
    
        if ($before < 2 * $MINUTE)
        {
            return "a minute ago";
        }
    
        if ($before < 45 * $MINUTE)
        {
            return floor($before / 60) . " minutes ago";
        }
    
        if ($before < 90 * $MINUTE)
        {
            return "an hour ago";
        }
    
        if ($before < 24 * $HOUR)
        {
    
            return (floor($before / 60 / 60) == 1 ? 'about an hour' : floor($before / 60 / 60).' hours'). " ago";
        }
    
        if ($before < 48 * $HOUR)
        {
            return "yesterday";
        }
    
        if ($before < 30 * $DAY)
        {
            return floor($before / 60 / 60 / 24) . " days ago";
        }
    
        if ($before < 12 * $MONTH)
        {
    
            $months = floor($before / 60 / 60 / 24 / 30);
            return $months <= 1 ? "one month ago" : $months . " months ago";
        }
        else
        {
            $years = floor  ($before / 60 / 60 / 24 / 30 / 12);
            return $years <= 1 ? "one year ago" : $years." years ago";
        }
    
        return "$time";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to know what would be a good way for storing the information
Just wanted to know if this is a good idea: I want to use
Possible Duplicate: Conditional Statements difference I wanted to know what is a good way
Just wanted to know if it was a good JavaScript practice. Let's say I
I wanted to know if anyone knows about a good way to update,constructors, equals,hash,to
Wanted to know if there was a way one could query shelveset details from
would any of you know a good way to put this into an associative
I wanted to know if this was thread safe/ good practice. My IOC is
I want to use the function findwindowex (windows API) I wanted to know what
I want to make an mmorpg game and i wanted to know some things

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.