def timespan(counter, ago=False):
    """Return approximate timespan for a number of seconds."""
    counter = int(counter)
    if counter == 0:
        return 'just now'
    for unit, count in (('second', 60), ('minute', 60), ('hour', 24), (
        'day', 7), ('week', 4), ('month', 12), ('year', 0)):
        if count > 0 and counter >= count * 2:
            counter /= count
        else:
            break
    return '%d %s%s%s' % (counter, unit, 's' if counter > 1 else '', ' ago' if
        ago else '')
