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 8424127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:54:18+00:00 2026-06-10T03:54:18+00:00

I use Codeigniter and it has the timespan() function that returns the time as

  • 0

I use Codeigniter and it has the timespan() function that returns the time as 1 Year, 10 Months, 2 Weeks, 5 Days, 10 Hours, 16 Minutes.

What I’d like to do is only show the time formatted in x hours ago if the time is within the last 24 hours, otherwise just show a normal datetime.

I feel like there’s got to be a function already made to do this but I haven’t had any luck finding it.

This is the timespan function included with Codeigniter, how can I alter it?

/**
 * Timespan
 *
 * Returns a span of seconds in this format:
 *  10 days 14 hours 36 minutes 47 seconds
 *
 * @access  public
 * @param   integer a number of seconds
 * @param   integer Unix timestamp
 * @return  integer
 */
if ( ! function_exists('timespan'))
{
    function timespan($seconds = 1, $time = '')
    {
        $CI =& get_instance();
        $CI->lang->load('date');

        if ( ! is_numeric($seconds))
        {
            $seconds = 1;
        }

        if ( ! is_numeric($time))
        {
            $time = time();
        }

        if ($time <= $seconds)
        {
            $seconds = 1;
        }
        else
        {
            $seconds = $time - $seconds;
        }

        $str = '';
        $years = floor($seconds / 31536000);

        if ($years > 0)
        {
            $str .= $years.' '.$CI->lang->line((($years > 1) ? 'date_years' : 'date_year')).', ';
        }

        $seconds -= $years * 31536000;
        $months = floor($seconds / 2628000);

        if ($years > 0 OR $months > 0)
        {
            if ($months > 0)
            {
                $str .= $months.' '.$CI->lang->line((($months   > 1) ? 'date_months' : 'date_month')).', ';
            }

            $seconds -= $months * 2628000;
        }

        $weeks = floor($seconds / 604800);

        if ($years > 0 OR $months > 0 OR $weeks > 0)
        {
            if ($weeks > 0)
            {
                $str .= $weeks.' '.$CI->lang->line((($weeks > 1) ? 'date_weeks' : 'date_week')).', ';
            }

            $seconds -= $weeks * 604800;
        }

        $days = floor($seconds / 86400);

        if ($months > 0 OR $weeks > 0 OR $days > 0)
        {
            if ($days > 0)
            {
                $str .= $days.' '.$CI->lang->line((($days   > 1) ? 'date_days' : 'date_day')).', ';
            }

            $seconds -= $days * 86400;
        }

        $hours = floor($seconds / 3600);

        if ($days > 0 OR $hours > 0)
        {
            if ($hours > 0)
            {
                $str .= $hours.' '.$CI->lang->line((($hours > 1) ? 'date_hours' : 'date_hour')).', ';
            }

            $seconds -= $hours * 3600;
        }

        $minutes = floor($seconds / 60);

        if ($days > 0 OR $hours > 0 OR $minutes > 0)
        {
            if ($minutes > 0)
            {
                $str .= $minutes.' '.$CI->lang->line((($minutes > 1) ? 'date_minutes' : 'date_minute')).', ';
            }

            $seconds -= $minutes * 60;
        }

        if ($str == '')
        {
            $str .= $seconds.' '.$CI->lang->line((($seconds > 1) ? 'date_seconds' : 'date_second')).', ';
        }

        return substr(trim($str), 0, -1);
    }
}
  • 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-10T03:54:20+00:00Added an answer on June 10, 2026 at 3:54 am

    This function will accept a string, a numeric (unix) timestamp, or a DateTime object. It also accepts jQuery.now(). Time may be in the future or past.

    function time_ago($time=false, $just_now=false) {
        if ($time instanceOf DateTime)
            $time = $time->getTimestamp();
        elseif (is_numeric($time))
            $time = date('m/d/y h:i A', $time);
        if (strtotime($time) === false)
            $time = date('m/d/y h:i A', time());
        $interval =  date_create($time)->diff(date_create('now'));
        $adjective = strtotime($time) > time() ? 'from now' : 'ago';
        return (
            $interval->days > 0 ? 
                $time : (
                    $interval->h < 1  && $interval->i < 1 && $just_now ? 
                        'just now' : 
                        (
                            $interval->h > 1 ? 
                                $interval->h.' hour'.(
                                    $interval->h > 1 ? 
                                        's' : 
                                        ''
                                ).' ago' : 
                                $interval->i.' minutes'.' '.$adjective
                        )
                )
        );
    }
    
    
    echo time_ago('8/22/2012 5:00 PM'); // 3 hours ago
    echo time_ago('8/21/2012 5:00 PM'); // 8/21/2012 5:00 PM
    echo time_ago(time()); // 0 hours ago
    echo time_ago(time(), true); // just now
    echo time_ago(strtotime('5 days ago')); // 08/17/12 08:18 PM
    echo time_ago(strtotime('5 hours ago')); // 5 hours ago
    echo time_ago(strtotime('5 minutes ago')); // 5 minutes ago
    echo time_ago(strtotime('+5 minutes')); // 5 minutes from now
    
    echo time_ago('jQuery.now()', true); // just now
    echo time_ago('sweet explosions, bro!', true); // just now
    

    Documentation

    • date – http://php.net/manual/en/function.date.php
    • DateTime object – http://www.php.net/manual/en/book.datetime.php
    • DateInterval object – http://www.php.net/manual/en/class.dateinterval.php
    • is_numeric – http://php.net/manual/en/function.is-numeric.php
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Has anybody tried out simpleloginsecure authentication library for codeigniter? I like that it has
I noticed that CodeIgniter has implemented the valid_email() function, in which they are using
I use auto CodeIgniter 2.1 + Auto Crumb helper that I get from here
I'm trying to use the CodeIgniter DB functionality (as it's DB(...) function, defined in
I have got a little problem using the CodeIgniter route function. I use the
CodeIgniter has a method $this->load->vars($array) that is ideally used in the parent Controller to
I'm creating a form in CodeIgniter that has a paired value- Companies, and CEOs.
Reading all about PHP frameworks, CodeIgniter seems really nice in that I like it's
I know that Codeigniter has a very useful security class which can prevent CSRF/XSRF
I'm developing a mailing queue in Codeigniter that sends 100 messages at a time.

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.