【问题标题】:date / time convert to time since php日期/时间转换为 PHP 以来的时间
【发布时间】:2011-02-26 11:57:29
【问题描述】:

你好在我的数据库日期/时间是这种格式

2010-06-01T18:20:25+0000

我想回应自那个日期/时间以来经过的时间

例如

4 天 3 小时 36 分 4 秒

这可能吗?

【问题讨论】:

    标签: php date time


    【解决方案1】:

    我为不到一分钟前发布的情况更改了上述代码的一行。

        function ax_getRoughTimeElapsedAsText($iTime0, $iTime1 = 0)
        {
        if ($iTime1 == 0) { $iTime1 = time(); }
        $iTimeElapsed = $iTime1 - $iTime0;
    
        if ($iTimeElapsed < (60)) {
            return "Less than a minute ago";
        } else if ($iTimeElapsed < (60*60)) {
            $iNum = intval($iTimeElapsed / 60); $sUnit = "minute";
        } else if ($iTimeElapsed < (24*60*60)) {
            $iNum = intval($iTimeElapsed / (60*60)); $sUnit = "hour";
        } else if ($iTimeElapsed < (30*24*60*60)) {
            $iNum = intval($iTimeElapsed / (24*60*60)); $sUnit = "day";
        } else if ($iTimeElapsed < (365*24*60*60)) {
            $iNum = intval($iTimeElapsed / (30*24*60*60)); $sUnit = "month";
        } else {
            $iNum = intval($iTimeElapsed / (365*24*60*60)); $sUnit = "year";
        }
    
        return $iNum . " " . $sUnit . (($iNum != 1) ? "s" : "") . " ago";
        }
    

    【讨论】:

      【解决方案2】:
      function time_ago($timestamp, $granularity = 2) {
        $timestamp = time() - $timestamp;
        $units = array('1 year|%d years' => 31536000, 
                       '1 week|%d weeks' => 604800, 
                       '1 day|%d days' => 86400, 
                       '1 hour|%d hours' => 3600, 
                       '1 min|%d mins' => 60, 
                       '1 sec|%d secs' => 1
                      );
        $output = '';
        foreach ($units as $key => $value) {
          $key = explode('|', $key);
          if ($timestamp >= $value) {
            $pluralized = floor($timestamp / $value) > 1 ? 
                          sprintf($key[1], floor($timestamp / $value)) : 
                          $key[0];
            $output .= ($output ? ' ' : '') . $pluralized;
            $timestamp %= $value;
            $granularity--;
          }
          if ($granularity == 0) {
            break;
          }
        }
        return $output ? $output : "Just now";
      }
      

      这应该很接近。

      编辑:添加了这一行: $timestamp = time() - $timestamp;

      【讨论】:

      • 我似乎可以让这个功能工作?我正在尝试:$c = function time_ago("1274744330","2"); echo "$c
        ";我错了吗?非常感谢
      • 那是错误的。 $c = time_ago("1274744330","2"); echo "$c
        "; (注意缺少“功能”关键字?)我得到:40 年 21 周
      • 是的,意识到 40 年是错误的。忘记计算,答案已编辑。现在我得到 1 周 21 小时的粒度 2 和 1 周 21 小时 37 分 24 秒的全粒度。
      【解决方案3】:

      下面是我为此编写的一个函数。随意使用。

      /**
       * Returns rough (in largest single unit) time elapsed between two times.
       * @param int $iTime0  Initial time, as time_t.
       * @param int $iTime1  Final time, as time_t. 0=use current time.
       * @return string Time elapsed, like "5 minutes" or "3 days" or "1 month".
       *              You might print "ago" after this return if $iTime1 is now.
       * @author Dan Kamins - dos at axonchisel dot net
       */
      function ax_getRoughTimeElapsedAsText($iTime0, $iTime1 = 0)
      {
          if ($iTime1 == 0) { $iTime1 = time(); }
          $iTimeElapsed = $iTime1 - $iTime0;
      
          if ($iTimeElapsed < (60)) {
              $iNum = intval($iTimeElapsed); $sUnit = "second";
          } else if ($iTimeElapsed < (60*60)) {
              $iNum = intval($iTimeElapsed / 60); $sUnit = "minute";
          } else if ($iTimeElapsed < (24*60*60)) {
              $iNum = intval($iTimeElapsed / (60*60)); $sUnit = "hour";
          } else if ($iTimeElapsed < (30*24*60*60)) {
              $iNum = intval($iTimeElapsed / (24*60*60)); $sUnit = "day";
          } else if ($iTimeElapsed < (365*24*60*60)) {
              $iNum = intval($iTimeElapsed / (30*24*60*60)); $sUnit = "month";
          } else {
              $iNum = intval($iTimeElapsed / (365*24*60*60)); $sUnit = "year";
          }
      
          return $iNum . " " . $sUnit . (($iNum != 1) ? "s" : "");
      }
      

      要使用此函数,您需要先将时间转换为 time_t 格式(自“纪元”以来的整数 #seconds)。这些 PHP 函数中的任何一个都可能对此有所帮助:http://php.net/strptimehttp://php.net/strtotime

      【讨论】:

      • 问题在我发布后被编辑...我的函数只输出最大的时间单位,这通常是用户关心的,以及原始问题似乎在问什么。
      • 谢谢哥们,我设法像你说的那样转换为时间戳,但是输出只是“7天”它不输出分钟等?
      • @Webby,正如我所说,它只输出最大的时间块。如果时间在 61 秒和 59 分钟 + 59 秒之间,它将输出“分钟”(并且只有分钟)。
      • 实际上我最终决定使用您的功能,就像您说的所有用户都关心..我的要求结束了,杀死哈哈谢谢
      • 然后在我的函数中使用 $granularity = 1 。你喜欢那些 elseif() 语句吗?
      【解决方案4】:

      你可以获取那个 fron db 并使用 strtotime 函数,该函数将进入 epoch 。然后使用 date 命令将其打印成您想要的任何格式。

      【讨论】:

      • 您的解决方案与两次之间的时间间隔没有任何关系,这就是这里所要求的。
      【解决方案5】:

      您可以直接在数据库中使用 func timediff:

      http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff

      第一个参数作为日期,第二个参数作为 now()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多