views:

177

answers:

1

In a twitter RSS feed the pubDate is published as

Sat, Jun 5, 2010 19:20

Using PHP, whats the best way to convert this into the time lasped since published. For eaxmple,

posted 4 minutes ago posted 1 hour ago posted 4 hours ago posted 1 day ago posted 2 days ago posted 1 month ago posted 2 months ago

You help much appreciated

A: 
function how_long_ago($unixTime) {
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
    );
    $today = time();
    $since = $today - $unixTime;
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {
        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];
        if (($count = floor($since / $seconds)) != 0) {
            break;
        }
    }
    return $count == 1 ? '1 '.$name : "$count {$name}s";
}

$rssUnixTime = strtotime('Sat, Jun 5, 2010 19:20');
echo 'posted '.how_long_ago($rssUnixTime).' ago';
Chris