문제

I need a suggestion how to convert Mon May 12 19:11:38 +0000 2014 into 2 minutes ago or 10 minutes ago.

My coding is something like this.

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>";

I've implemented something like this and I want to dig the created_at because it looks a bit weird. Anyway I've found many sources like this. But that one is not what I want. What I want is to make it show like 2 days ago, 10 days ago, yesterday. Any idea?

도움이 되었습니까?

해결책

This is what you are looking for.

    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;

    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top