自从发布Twitter推特获得时间 - php

时间:2014-05-02 10:40:01

标签: php twitter

我正在使用它来生成我的推特:

    # Access as an object
    $tweetText = $tweet->text;
    $tweetDate = $tweet->created_at;

    # Make links active
    $tweetText = preg_replace("/(http:\/\/|www.)(([^\s<]{4,68})[^\s<]*)/", '<a href="http://$2$3" target="_blank">$1$2$4</a>', $tweetText);

    # Linkify user mentions
    $tweetText = preg_replace("/@(w+)/", '<a href="http://www.twitter.com/$1" target="_blank">@$1</a>', $tweetText);

    # Linkify tags
    $tweetText = preg_replace("/#(w+)/", '<a href="http://search.twitter.com/search?q=$1" target="_blank">#$1</a>', $tweetText);

    # Output
    echo $tweetText;
    echo '<br><span>Posted: ';
    echo $tweetDate;
    echo '</span>';
    echo '<br> <br>';

它目前显示推文发布的日期,有没有办法让它显示推文发布的时间,例如4小时前而不是约会?

$tweetDate目前正在打印Tue Apr 29 09:43:17 +0000 2014

1 个答案:

答案 0 :(得分:1)

使用以下代码:

$tweetDate  =   strtotime($tweetDate); 

$diff   =   time()-$tweetDate;

if($diff<60){
    echo $diff." sec ago";
}else if($diff<(60*60)){
    echo round($diff/60)." minute ago";
}else if($diff<(60*60*24)){
    echo round($diff/(60*60))." hours ago";
}else{
    echo round($diff/(60*60*24))." days ago";
}