Time ago implementation in PHP

I have developed Ajax Search box for Codeforgeek and if you notice the result it shows the date in form of ‘about 5 days ago’. This is time ago implementation and since i use WordPress that’s why i made this on PHP.

The algorithm works in very simple way. It takes the date as an input and subtract it with the today’s date. Then it compare the remaining date with predefined set of rules which determine the month,day,minute and even year. Rules to determine ago time is as follows:

12 * 30 * 24 * 60 * 60 => ‘year’,
30 * 24 * 60 * 60 => ‘month’,
24 * 60 * 60 => ‘day’,
60 * 60 => ‘hour’,
60 => ‘minute’,
1 => ‘second’

It simply means that year is made up of 12 months with 30 days in each month,24 hours in each day,60 minutes in each hour and 60 seconds in each minute. Same goes for month and day and lastly to second. Here is a complete algorithm written in PHP.

<?php
function get_timeago( $ptime )
{
    $estimate_time = time() - $ptime;

    if( $estimate_time < 1 )
    {
        return 'less than 1 second ago';
    }

    $condition = array(
                12 * 30 * 24 * 60 * 60  =>  'year',
                30 * 24 * 60 * 60       =>  'month',
                24 * 60 * 60            =>  'day',
                60 * 60                 =>  'hour',
                60                      =>  'minute',
                1                       =>  'second'
    );

    foreach( $condition as $secs => $str )
    {
        $d = $estimate_time / $secs;

        if( $d >= 1 )
        {
            $r = round( $d );
            return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
        }
    }
}
?>

How to use it.

WordPress stored the publish date of post in this format ‘YYYY-MM-DD H:M:S’. To parse the date and use it like String i used in built function of PHP called ‘strtotime()’. This function takes the date as input return it into string format.
Here how i called the algorithm to extract the time ago.

/*
    * $row[] is array of rows from MySQL database of WordPress.
    * You can use the normal date function to pass it to algorithm.
*/

$timeago=get_timeago(strtotime($row['post_date']));
echo $timeago;

Demo application:

I have already developed Ajax search box located on top menu bar which search the posts from WordPress table and show the date as time ago basis.

Is date format matters for algorithm?

According to my tests, No. You can provide the date in any format but make sure you have pass it using strtotime() function.

Shahid (UnixRoot) Shaikh

Hey there, This is Shahid, an Engineer and Blogger from Bombay. I am also an Author and i wrote a programming book on Sails.js, MVC framework for Node.js.

Related Posts

16 Comments

  1. i am trying to convert mysql current time to time ago i have tried a lot but it always show 8 hours ago even give just now time please help me

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.