像 Facebook 和 Twitter 一样转换时间戳

Convert timestamp like Facebook and Twitter

我像这样从服务器获取时间:25-07-2015 12:25:28
现在我想这样显示:
a few second ago
1 minute ago
30 minutes ago
1 Hour ago
12 Hours ago
24 Hours ago
之后 告诉我那天的日期,例如:
25 August 2015

你看过php中的Date函数了吗?

以下代码有效。但没有完成数据验证(例如:旧>新)

 <?php
$olddate = "25-08-2015 15:35:28";       //date as string
$now = time();                  //pick present time from server     
$old = strtotime( $olddate);  //create integer value of old time
$diff =  $now-$old;             //calculate difference
$old = new DateTime($olddate);
$old = $old->format('Y M d');       //format date to "2015 Aug 2015" format

    if ($diff /60 <1)                       //check the difference and do echo as required
    {
    echo intval($diff%60)."seconds ago";
    }
    else if (intval($diff/60) == 1) 
    {
    echo " 1 minute ago";
    }
    else if ($diff / 60 < 60)
    {
    echo intval($diff/60)."minutes ago";
    }
    else if (intval($diff / 3600) == 1)
    {
    echo "1 hour ago";
    }
    else if ($diff / 3600 <24)
    {
    echo intval($diff/3600) . " hours ago";
    }
    else if ($diff/86400 < 30)
    {
    echo intval($diff/86400) . " days ago";
    }
    else
    {
    echo $old;  ////format date to "2015 Aug 2015" format
    }
?>

如果可以,请更改循环。逻辑保持不变。

    <?php
  function timeago($timestamp){
    $time_ago        = strtotime($timestamp);
    $current_time    = time();
    $time_difference = $current_time - $time_ago;
    $seconds         = $time_difference;

    $minutes = round($seconds / 60); // value 60 is seconds
    $hours   = round($seconds / 3600); //value 3600 is 60 minutes * 60 sec
    $days    = round($seconds / 86400); //86400 = 24 * 60 * 60;
    $weeks   = round($seconds / 604800); // 7*24*60*60;
    $months  = round($seconds / 2629440); //((365+365+365+365+366)/5/12)*24*60*60
    $years   = round($seconds / 31553280); //(365+365+365+365+366)/5 * 24 * 60 * 60

    if ($seconds <= 60){

      return "Just Now";

    } else if ($minutes <= 60){

      if ($minutes == 1){

        return "one minute ago";

      } else {

        return "$minutes minutes ago";

      }

    } else if ($hours <= 24){

      if ($hours == 1){

        return "an hour ago";

      } else {

        return "$hours hrs ago";

      }

    } else if ($days <= 7){

      if ($days == 1){

        return "yesterday";

      } else {

        return "$days days ago";

      }

    } else if ($weeks <= 4.3){

      if ($weeks == 1){

        return "a week ago";

      } else {

        return "$weeks weeks ago";

      }

    } else if ($months <= 12){

      if ($months == 1){

        return "a month ago";

      } else {

        return "$months months ago";

      }

    } else {

      if ($years == 1){

        return "one year ago";

      } else {

        return "$years years ago";

      }
    }
  }
?>