I have gotten many requests for this snippet which i use here on Netnaija.com to show time of actions as either x seconds ago, x minutes ago, x hours ago, x days ago, x months ago or x years ago. Its of course in PHP. So incase you need it here it is.

You should supply a UNIX Timestamp as $ptime. To use, you simply call $timeAsAgo = timeAgo($timeStamp);.

PHP Code

<?php 
function timeAgo($ptime)
{
    
$etime time() - $ptime;

    if (
$etime 1)
    {
        return 
'0 seconds';
    }

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

    foreach (
$a as $secs => $str)
    {
        
$d $etime $secs;
        if (
$d >= 1)
        {
            
$r round($d);
            return 
$r ' ' $str . ($r 's' '') . ' ago';
        }
    }
}
?>

Download Now
  • Date: Mar 24, 2014


I have gotten many requests for this snippet which i use here on Netnaija.com to show time of actions as either x seconds ago, x minutes ago, x hours ago, x days ago, x months ago or x years ago. Its of course in PHP. So incase you need it here it is.

You should supply a UNIX Timestamp as $ptime. To use, you simply call $timeAsAgo = timeAgo($timeStamp);.

PHP Code

<?php 
function timeAgo($ptime)
{
    
$etime time() - $ptime;

    if (
$etime 1)
    {
        return 
'0 seconds';
    }

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

    foreach (
$a as $secs => $str)
    {
        
$d $etime $secs;
        if (
$d >= 1)
        {
            
$r round($d);
            return 
$r ' ' $str . ($r 's' '') . ' ago';
        }
    }
}
?>


Gerardcole

i dnt understnd this statement, you make here: You should supply a UNIX Timestamp as $ptime. To use, you simply call$timeAsAgo = timeAgo($timeStamp); . Pls elucide wel pls

  • Date: Nov 06, 2014
  • Edited: Nov 06, 2014 by jdwhyte


I have gotten many requests for this snippet which i use here on Netnaija.com to show time of actions as either x seconds ago, x minutes ago, x hours ago, x days ago, x months ago or x years ago. Its of course in PHP. So incase you need it here it is.

You should supply a UNIX Timestamp as $ptime. To use, you simply call $timeAsAgo = timeAgo($timeStamp);.

PHP Code

<?php 
function timeAgo($ptime)
{
    
$etime time() - $ptime;

    if (
$etime 1)
    {
        return 
'0 seconds';
    }

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

    foreach (
$a as $secs => $str)
    {
        
$d $etime $secs;
        if (
$d >= 1)
        {
            
$r round($d);
            return 
$r ' ' $str . ($r 's' '') . ' ago';
        }
    }
}
?>


Gerardcole

Boss Please I don't really understand this and I'm so much in need of it. Please kindly explain further for pbnl

  • Date: Nov 07, 2014


Mine is showing 46years ago

jdwhyte

Because you didn't supply the $unix_timestamp to be calculated with

  • Date: Nov 07, 2014


Because you didn't supply the $unix_timestamp to be calculated with

Gerardcole

Boss please kindly help me make it work here so i can fix it up... Thanks alot

view.php (4KB) [6 views]
  • Date: Aug 10, 2017


Because you didn't supply the $unix_timestamp to be calculated with

Gerardcole
PHP Code

<?php 
define
TIMEBEFORE_NOW        'Just now' );
    defineTIMEBEFORE_MINUTE     '{num} minute ago' );
    defineTIMEBEFORE_MINUTES    '{num} minutes ago' );
    defineTIMEBEFORE_HOUR       '{num} hour ago' );
    defineTIMEBEFORE_HOURS      '{num} hours ago' );
    defineTIMEBEFORE_YESTERDAY  'Yesterday' );
    defineTIMEBEFORE_FORMAT     '%e %b' );
    defineTIMEBEFORE_FORMAT_YEAR'%e %b, %Y' );
    function time_ago$time )
    {
        $out    ''// what we will print out
        $now    time(); // current time
        $diff   $now $time// difference between the current and the provided dates
        if( $diff 60 // it happened now
            return TIMEBEFORE_NOW;
        elseif( $diff 3600 // it happened X minutes ago
            return str_replace'{num}', ( $out round$diff 60 ) ), $out == TIMEBEFORE_MINUTE TIMEBEFORE_MINUTES );
        elseif( $diff 3600 24 // it happened X hours ago
            return str_replace'{num}', ( $out round$diff 3600 ) ), $out == TIMEBEFORE_HOUR TIMEBEFORE_HOURS );
        elseif( $diff 3600 24 // it happened yesterday
            return TIMEBEFORE_YESTERDAY;
        else // falling back on a usual date format as it happened later than yesterday
            return strftimedate'Y'$time ) == date'Y' ) ? TIMEBEFORE_FORMAT TIMEBEFORE_FORMAT_YEAR$time );
    }
?>