Twitter API格式" created_at"用PHP

时间:2014-05-12 20:29:19

标签: php twitter

我需要一个建议如何将 Mon May 12 19:11:38 +0000 2014 转换为 2分钟前 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,因为它看起来有点奇怪。无论如何,我发现了许多来源,如this。但那个不是我想要的。我想要的是让它显示为2 days ago10 days agoyesterday。有什么想法吗?

1 个答案:

答案 0 :(得分:5)

这就是你要找的东西。

    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;

    }