自时间戳以来的天,小时,分钟

时间:2013-11-24 04:24:04

标签: php

所以我有这个功能,显示自时间戳以来的时间。

现在,如果让我们说60分钟,它会过去并显示“1小时前”。如果它是24小时,它会转到“1天前”,依此类推。我怎么能这样做,如果它是例如1小时,也显示分钟?

Event occured 58 minutes ago
3 minutes later
Event occured 1 hour 1 minute ago
26 hours later
Event occured 1 day 2 hour ago




function humanTiming($time) 
{

$time = time() - $time; // to get the time since that moment

$tokens = array (
    31536000 => 'year',
    2592000 => 'month',
    604800 => 'week',
    86400 => 'day',
    3600 => 'hour',
    60 => 'minute',
    1 => 'second'
);

foreach ($tokens as $unit => $text) {
    if ($time < $unit) continue;
    $numberOfUnits = floor($time / $unit);
    return $numberOfUnits.$text.' ago';
}

}

6 个答案:

答案 0 :(得分:2)

您需要像这样修改您的功能:

function humanTiming($time)
{
    $time = time() - $time; // to get the time since that moment

    $tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );

    $result = '';
    $counter = 1;
    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        if ($counter > 2) break;

        $numberOfUnits = floor($time / $unit);
        $result .= "$numberOfUnits $text ";
        $time -= $numberOfUnits * $unit;
        ++$counter;
    }

    return "{$result}ago";
}

检查codepad.org上的结果。

答案 1 :(得分:2)

PHP 5.2.0引入了DateTime对象,这将为该问题提供更好的解决方案。

我将在另一个问题上使用my answer来展示它是多么容易,但会略微编辑代码:

$create_time = "2016-08-02 12:35:04";
$current_time="2016-08-02 16:16:02";

$dtCurrent = DateTime::createFromFormat('Y-m-d H:i:s', $current_time);
// to use current timestamp, use the following:
//$dtCurrent = new DateTime();
$dtCreate = DateTime::createFromFormat('Y-m-d H:i:s', $create_time);
$diff = $dtCurrent->diff($dtCreate);

$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");

$interval将返回0 years 0 months 0 days 3 hours 40 minutes 58 seconds

现在让我们摆脱那些零值。好的旧正则表达式在这里非常有用:

$interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);

最后,我们得到了我们所需要的:3 hours 40 minutes 58 seconds

答案 2 :(得分:0)

您可以使用diff找到事件发生时间和当前时间差异。例如,请参阅以下代码

$time1 = new DateTime('2013-11-21 12:59:00'); // Event occurred time
$time2 = new DateTime(date('Y-m-d H:i:s')); // Current time
$interval = $time1->diff($time2);

echo $interval->y . " Year " .$interval->m . " Month " .$interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds <br/>";

答案 3 :(得分:0)

您可以使用渐进式计算方法,例如:

$tokens = array (
    31536000 => 'year',
    2592000 => 'month',
    604800 => 'week',
    86400 => 'day',
    3600 => 'hour',
    60 => 'minute',
    1 => 'second'
);

$tokenValues = array();
foreach($tokens as $tokenValue => $tokenName)
{
    if($time > $tokenValue)
    {
        $tokenValues[$tokenName] = floor($time \ $tokenValue);
        $time -= $tikenValue;
    }
    if(count($tokenValues) == 2)
    {
        break;
    }
}

然后你应该只有2个物品在tokenValues中,根据你的需要进行调整!

答案 4 :(得分:0)

我没有时间解释,但这个功能在我测试时起作用。另外,我添加了一个功能来处理当前时间和未来的时间。如果您为此函数提供精确的当前时间,它将返回'just now'。如果您提供过去的时间,它将返回以'ago'结尾的字符串。如果您从未来提供时间,它将返回以'later'结尾的字符串。另一个特征是时间单位的自动单数/复数形式。亲自试试吧!

function timeEllapsedFrom($from) {
  $time = time() - $from;
  $diff = abs($time);
  $tokens = array (
    'year' => 31536000,
    'month' => 2592000,
    'week' => 604800,
    'day' => 86400,
    'hour' => 3600,
    'minute' => 60,
    'second' => 1
  );
  $result = array();
  foreach ($tokens as $id => $length) {
    $value = floor($diff/$length);
    if ($value) $result[] = "$value $id" . ($value > 1 ? 's' : '');
    $diff -= $length*$value;
  }
  if (!count($result)) return 'just now';
  return join(', ', $result) . ($time < 0 ? ' later' : ' ago');
}

您可以使用以下代码进行测试:

echo timeEllapsedFrom(time() - 604800 - 7200 - 480 - 24);

它将输出:

1 week, 2 hours, 8 minutes, 24 seconds ago

希望它有所帮助!

答案 5 :(得分:0)

使用DateTime课程。适用于过去和未来的时间戳,并在需要时纠正多元化:

function elapsedTime($time)
{
    date_default_timezone_set('UTC');
    $given_time = new DateTime("@$time");
    $current_time = new DateTime();
    $diff = $given_time->diff($current_time);
    $timemap = array('y' => 'year',
                     'm' => 'month',
                     'd' => 'day',
                     'h' => 'hour',
                     'i' => 'minute',
                     's' => 'second');
    $timefmt = array();

    foreach ($timemap as $prop => $desc) {
        if ($diff->$prop > 0) {
            $timefmt[] = ($diff->$prop > 1)
                ? "{$diff->$prop} {$desc}s"
                : "{$diff->$prop} $desc";
        }
    }

    return (!$timefmt)
        ? 'just now'
        : ((count($timefmt) > 1)
            ? $diff->format(sprintf('%s and %s',
                implode(', ', array_slice($timefmt, 0, -1)), end($timefmt)))
            : end($timefmt))
            . (($given_time < $current_time) ? ' ago' : ' later');
}

测试:

var_dump(elapsedTime(strtotime('1987-11-25 11:40')));
# string(69) "25 years, 11 months, 29 days, 17 hours, 50 minutes and 42 seconds ago"
var_dump(elapsedTime(time()));
# string(8) "just now"
var_dump(elapsedTime(strtotime('2013-11-25')));
# string(41) "18 hours, 29 minutes and 18 seconds later"