日期/时间转换为自PHP以来的时间

时间:2010-06-01 18:43:25

标签: php date time

我的数据库日期/时间的Hello是这种格式

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

我想回应那个从那个日期/时间开始经过的时间

e.g。

4天3小时36分4秒

这可能吗?

5 个答案:

答案 0 :(得分:4)

下面是我写的一个函数。随意使用它。

/**
 * 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" : "");
}

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

答案 1 :(得分:2)

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

    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 :(得分:1)

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

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

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

答案 3 :(得分:1)

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;

答案 4 :(得分:-1)

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