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

  • SEARCH
  • Home
  • 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 9265975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:12:34+00:00 2026-06-18T14:12:34+00:00

I got a script that saves a timestamp (using time() function) every time a

  • 0

I got a script that saves a timestamp (using time() function) every time a user does a special action.

I want to show on a page how long ago a user did this action, in an easy to read user way. I currently got it to show how many hours ago it approximately was, but I’d like it to return in a way looking like this:

1 day, 5 hours and 30 mintues way…

  • 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-06-18T14:12:36+00:00Added an answer on June 18, 2026 at 2:12 pm

    To achieve what you want, you can simply do

    $target = strtotime("Feb 7 2013 2:15 AM");
    $now = time();
    $diff = $now - $target;
    $days = floor(($diff)/24/60/60);
    $hours = floor(fmod($diff, 24*60*60)/60/60);
    $minutes = floor(fmod(fmod($diff, 24*60*60),60*60)/60);
    echo $days . " days " . $hours . " hours " . $minutes . " minutes"; // e.g. "2 days 14 hours 54 minutes"
    

    A more versatile solution

    function timeago($timestamp) {
    
        // Predefine variables
        $timeago = '';
        $past = FALSE;
        $future = FALSE;
        $prefixAgo = NULL;
        $prefixFromNow = NULL;
        $suffixAgo = "ago";
        $suffixFromNow = "from now";
        $wordSeperator = " ";
    
        // Current time
        $current = time();
    
        // Seconds ago
        $seconds = abs($current - $timestamp);
    
        // Determine whether the time is in the future or the past
        if ($current >= $timestamp) {
            $past = TRUE;
        } else {
            $future = TRUE;
        }
    
        // Calculate time ago in different formats
        $minutes = $seconds / 60;
        $hours = $minutes / 60;
        $days = $hours / 24;
        $months = $days / 30;
        $years = $days / 365;
    
        // Format time ago according to calculated time
        if      ($seconds < 45) { $timeago = "less than a minute"; }
        else if ($seconds < 90) { $timeago = "about a minute"; }
        else if ($minutes < 45) { $timeago = round($minutes) . " minutes"; }
        else if ($minutes < 90) { $timeago = "about an hour"; }
        else if ($hours < 24)   { $timeago = "about " . round($hours) . " hours"; }
        else if ($hours < 42)   { $timeago = "a day"; }
        else if ($days < 30)    { $timeago = round($days) . " days"; }
        else if ($days < 45)    { $timeago = "about a month"; }
        else if ($days < 365)   { $timeago = round($months) . " months"; }
        else if ($years < 1.5)  { $timeago = "about a year"; }
        else                    { $timeago = round($years) . " years"; }
    
        // Append prefix and suffix
        if (!empty($prefixAgo))     { $prefixAgo        = $prefixAgo . $wordSeperator; }
        if (!empty($suffixAgo))     { $suffixAgo        = $wordSeperator . $suffixAgo; }
        if (!empty($prefixFromNow)) { $prefixFromNow    = $prefixFromNow . $wordSeperator; }
        if (!empty($suffixFromNow)) { $suffixFromNow    = $wordSeperator . $suffixFromNow; }
        if ($past)                  { $timeago = $prefixAgo . $timeago . $suffixAgo; }
        if ($future)                { $timeago = $prefixFromNow . $timeago . $suffixFromNow; }
    
        // Return results
        if (!empty($timeago)) {
            return $timeago;
        } else {
            return NULL;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a script that sets some session values before redirecting to / using
So I'm making a script and I got a function: loot() that returns: {3
I've got a script that produces several reports, each of which I've been converting
I've got a script that changes the background colour of text that has been
I've got a script that reads in from a CSV file and sometimes the
I've got a script that is capable of building up a longish transaction of
I've got a bash script that can take arguments in a variety of different
I have got a demo script that lets me authorize with my app and
I've got a script control that has a control embedded within it that exposes
I've got a bash script that reads input from a file like this: while

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.