duanban4769 2014-05-02 02:40
浏览 55
已采纳

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

I'm using this to generate my twitter feed:

    # 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>';

It currently shows the date the tweet was posted, is there a way to get it to show how long ago the tweet was posted, e.g 4 hours ago instead of a date?

$tweetDate Currently prints Tue Apr 29 09:43:17 +0000 2014

  • 写回答

1条回答 默认 最新

  • dongyuan7110 2014-05-02 02:44
    关注

    Use below code :

    $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";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部