from datetime import datetime


def time_ago(date):

    """
    Returns a string like '3 minutes ago' or '8 hours ago'

    Got this from joltz in #python on freenode.  Thanks!

    >>> time_ago(datetime.now() - timedelta(seconds=90))
    'one minute ago'
    >>> time_ago(datetime.now() - timedelta(seconds=900))
    '15 minutes ago'
    >>> time_ago(datetime.now() - timedelta(seconds=60*12))
    '12 minutes ago'
    >>> time_ago(datetime.now() - timedelta(seconds=60*60*4))
    'about 4 hours ago'
    """

    current_date = datetime.now()
    hours_ago = (current_date - date).seconds / 60 / 60
    minutes_ago = (current_date - date).seconds / 60
    days_ago = (current_date - date).days

    if days_ago > 0: # More than 24 hours ago
        return date.strftime('on %h %d, %Y at %I:%M%p')

    if hours_ago >= 1 and hours_ago <= 23: # An hour or more ago
        if hours_ago > 1:
            return "about %s hours ago" % str(hours_ago)
        else:
            return "about an hour ago"

    if minutes_ago >= 0 and minutes_ago <= 59: # Less than an hour ago
        if minutes_ago > 1:
            return "%s minutes ago" % str(minutes_ago)
        elif minutes_ago == 1:
            return "one minute ago"
        else:
            return "just now"