duanban4769 2014-05-02 10: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 10: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";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里