You will need to do some counting, no way to skip that, you'll just have to keep it as clear as possible while still maintaining a valid result in all situations (e.g. avoid getting strings like "1 days").
I'd suggest starting from the following snippet written on php.net by acrion@gmail.com:
function pluralize( $count, $text )
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
function ago( $datetime )
{
$interval = date_create('now')->diff( $datetime );
$suffix = ( $interval->invert ? ' ago' : '' );
if ( $v = $interval->y >= 1 ) return pluralize( $interval->y, 'year' ) . $suffix;
if ( $v = $interval->m >= 1 ) return pluralize( $interval->m, 'month' ) . $suffix;
if ( $v = $interval->d >= 1 ) return pluralize( $interval->d, 'day' ) . $suffix;
if ( $v = $interval->h >= 1 ) return pluralize( $interval->h, 'hour' ) . $suffix;
if ( $v = $interval->i >= 1 ) return pluralize( $interval->i, 'minute' ) . $suffix;
return pluralize( $interval->s, 'second' ) . $suffix;
}
Source
So you'd need something like that:
function pluralize( $count, $text )
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
function ago( $datetime )
{
$interval = date_create('now')->diff( $datetime );
$suffix = ( $interval->invert ? ' ago' : '' );
if ( $v = $interval->y >= 1 ) return pluralize( $interval->y, 'year' ) . $suffix;
if ( $v = $interval->m >= 1 ) return pluralize( $interval->m, 'month' ) . $suffix;
if ( $v = $interval->d >= 1 ) return pluralize( $interval->d, 'day' ) . $suffix;
if ( $v = $interval->h >= 1 ) return pluralize( $interval->h, 'hour' ) . $suffix;
if ( $v = $interval->i >= 1 ) return pluralize( $interval->i, 'minute' ) . $suffix;
return pluralize( $interval->s, 'second' ) . $suffix;
}
$last_active = new DateTime($user['last_active']);
$interval = pluralize($last_active);
This will work both for positive and negative differences, though if you prefer avoiding the "ago" suffix you can freely remove it.