Source:
function pretty_date($date, array $params = null) {
    if (empty($date)) {
        return '';
    }

    if (strstr($date, '-')) {
        $date = str_replace('-', '/', $date);
    }

    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths = array("60","60","24","7","4.35","12","10");

    $now = time();
    if (!is_numeric($date)) {
        $unix_date = strtotime($date);
    } else {
        $unix_date = $date;
    }
    if (empty($unix_date)) {
        return '';
    }	    
    if (isset($params['format'], $params['format_days'])) {
        $diff = abs($unix_date - time());
        if ((3600 * 24 * $params['format_days']) >= $diff) {
            return date($params['format'], $unix_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}";
}

Converts a timestamp or datetime to a nice looking time that make sense like 5 days ago, 3 minutes ago etc.

Pythoneer 3 years ago