Twitter API格式"created_at"在PHP中


Twitter API format "created_at" in PHP

我需要一个建议如何转换Mon May 12 19:11:38 +0000 20142分钟前10分钟前

我的代码是这样的。

if(isset($tweets->statuses) && is_array($tweets->statuses)) {
            if(count($tweets->statuses)) {
                foreach($tweets->statuses as $tweet) { 
                    echo "<td>";
                    echo $tweet->user->screen_name; 
                    echo "</td>"; 
                    echo "<td>";
                    echo $tweet->text;
                    echo "</td>";
                    echo "<td>";
                    echo $tweet->created_at;
                    echo "</td>";
                echo "</tr>";

我已经实现了这样的东西,我想挖掘created_at,因为它看起来有点奇怪。总之,我找到了很多这样的资料。但那不是我想要的。我想让它看起来像2 days ago 10 days ago yesterday。任何想法?

这就是你要找的。

    function timeSince($time) {
        $since = time() - strtotime($time);
        $string     = '';
        $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'),
            array(1 , 'second')
        );
        for ($i = 0, $j = count($chunks); $i < $j; $i++) {
            $seconds = $chunks[$i][0];
            $name = $chunks[$i][1];
            if (($count = floor($since / $seconds)) != 0) {
                break;
            }
        }
        $string = ($count == 1) ? '1 ' . $name . ' ago' : $count . ' ' . $name . 's ago';
        return $string;
    }