Php 转换时间戳,如Facebook和Twitter

Php 转换时间戳,如Facebook和Twitter,php,timestamp,Php,Timestamp,我是这样从服务器获取时间的:25-07-2015 12:25:28 现在我想这样展示它: 几秒钟前 1分钟前 30分钟前 1小时前 12小时前 24小时前之后 向我显示那天的日期,如: 2015年8月25日您看过php中的函数了吗?以下代码有效。但未进行数据验证(例如:旧>新) 如果可以,请更改循环。逻辑保持不变。 <?php function timeago($timestamp){ $time_ago = strtotime($timestamp)

我是这样从服务器获取时间的:
25-07-2015 12:25:28

现在我想这样展示它:
几秒钟前
1分钟前

30分钟前

1小时前

12小时前

24小时前
之后

向我显示那天的日期,如:

2015年8月25日

您看过php中的函数了吗?

以下代码有效。但未进行数据验证(例如:旧>新)


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


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

      }
    }
  }
?>

到目前为止,您尝试了什么?我们并不是来为您编写代码的——一旦您有什么问题,我们可以帮助您,我尝试了很多,但没有找到我想要的那种类型的脚本。所有像这样的脚本在2年前和10年前没有人告诉我确切的日期。删除了我的注释,因为答案更新了所有要求。@user3396968:现在检查它。Intval($diff)未更新。并且删除你的评论。谢谢你,伙计,你节省了我的时间。工作起来很有魅力:*再次感谢
    <?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";

      }
    }
  }
?>