Jun
10
2011

The PHP Time Ago Function

It’s often handy to refer to Gmail’s “00:00 (4 minutes ago)” example as an additional feature of usability for web users.  By creating an effective time-ago function using PHP, we can effectively format most MySQL dates, datetime, and timestamp entries into an easier to read and comprehend “Sometime ago” outputs.

Besides, who really wants to have to read a date anymore?!

Accepted MySQL Dates

As MySQL is capable of storing dates and datetimes in a number of ways, below are examples of the MySQL time-related fieldtypes that will function properly using the time-ago function below.

  • Date (i.e. YYYY-MM-DD)
  • Datetime / Timestamp (YYYY-MM-DD HH:MM:SS or YY-MM-DD HH:MM:SS)

More information on MySQL date and time functions can be found here.

The Time-Ago Function

The function itself is fairly simple, and though it was not written by me (terribly sorry- I cannot remember whom to provide credit to here!), is very easy to decipher.

function time_ago( $date )
{
    if( empty( $date ) )
    {
        return "No date provided";
    }

    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");

    $lengths = array("60","60","24","7","4.35","12","10");

    $now = time();

    $unix_date = strtotime( $date );

    // check validity of date

    if( empty( $unix_date ) )
    {
        return "Bad date";
    }

    // is it future date or past date

    if( $now > $unix_date )
    {
        $difference = $now - $unix_date;
        $tense = "ago";
    }
    else
    {
        $difference = $unix_date - $now;
        $tense = "from now";
    }

    for( $j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++ )
    {
        $difference /= $lengths[$j];
    }

    $difference = round( $difference );

    if( $difference != 1 )
    {
        $periods[$j].= "s";
    }

    return "$difference $periods[$j] {$tense}";

}

Important: How to Use

Calling the function is the simple part (assuming of course your dates are properly formatted), and can be done inline with your scripts using the example below:

echo time_ago(yourtimehere);

If you prefer to turn the call into a PHP constant on your page…

$timeago = time_ago(yourtimehere);

Then simply include the “$timeago” reference within your (what is presumably) repeated time ago output.

13 Comments + Add Comment

  • I have a mysql table output the first output is fine but the second and so forth shows fatal error cannot redeclare time_ago() any ideas?

  • My PHP time ago tutorial is moved here: http://www.allphptricks.com/php-time-ago-function/

  • Bennett this is really helpful tutorial, thanks to share with us :)
    I also recently wrote another PHP time ago function, hope this also help.
    https://htmlcssphptutorial.wordpress.com/2015/08/21/time-ago-php-function/

  • Thanks it helps a lot :D

  • Brilliant! Thank you.

  • Hi,
    The script above works fine but when i post , it shows 3 hrs from now. it should be 1 sec ago..

    any idea????

    • hey check your php timezone! :P

  • Thanks buddy. This is a great piece of code. You’re pretty smart. Hope you often post pieces of code like this.

    THANKS.

    Yan. P

  • Excellent !!!!!!! Excellent !!!!!!! Excellent !!!!!!! Excellent !!!!!!! Excellent !!!!!!! Excellent !!!!!!!
    A++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  • Great function! Thanks a lot!

  • Hi, your script works great…

    only one problem…when i post a comment it show around 29 secs straight after i post….
    itshoud be 1 or 2 sec’s ago…

    any ideas????

    • It appears that would be related to the $lengths array as it doesn’t contain 1 second increment values (see lines 13, 40, and 42 in the code above where the actual differences are calculated)

  • i use this class

    it is good

Leave a comment