2

我有以下时间戳1347216222,它是当天的,并且正在使用它与一个time_since函数来检查它在几小时之前是多久......分钟等。

<?php

/* Works out the time since the entry post, takes a an argument in unix time (seconds) */
function time_since($original) {
    // array of time period chunks
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
    );

    $today = time(); /* Current unix time  */
    $since = $today - $original;

    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {

        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];

        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0) {
            // DEBUG print "<!-- It's $name -->\n";
            break;
        }
    }

    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";

    if ($i + 1 < $j) {
        // now getting the second item
        $seconds2 = $chunks[$i + 1][0];
        $name2 = $chunks[$i + 1][1];

        // add second item if it's greater than 0
        if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
            $print .= ($count2 == 1) ? ', 1 '.$name2 : ", $count2 {$name2}s";
        }
    }
    return $print;
}

echo time_since(1347216222);

?>

输出是-1 years, 12 months。谁能帮我解决这个问题?

4

1 回答 1

0

我定制了要使用的函数DateTimeand DateInterval,这使它更具可读性。

它与您发布的关于如何处理时差的功能不同。在您的情况下,您测试了两次值(每个“单位”一次)。这需要您进行复杂的计算。

另一方面,下面的函数利用了这样一个事实,即为DateInterval您提供年、月、日、小时、分钟和秒的即用型差异(例如,2015 年和 2014 年之间的差异是 1 年和0 秒,而不是 UNIX 时间戳)。此外,使用DateTime允许您优雅地处理时区。

话虽如此,您只需要关心如何打印差异,这正是for循环的用途 - 我们提取两个值并使用额外的项目(我们称此类项目监护人),以便在我们保存额外的条件时想提取第二项。

这是功能:

<?php
function pluralize($number, $unit) {
    return $number . ' ' . $unit . ($number !== 1 ? 's' : '');
}

function time_since(DateTime $original) {
    $now = new DateTime('now');
    $diff = $now->diff($original);

    //is from the past?
    if ($diff->invert) {
        $chunks = [
            [$diff->y, 'year'],
            [$diff->m, 'month'],
            [$diff->d, 'day'],
            [$diff->h, 'hour'],
            [$diff->i, 'minute'],
            [$diff->s, 'second'],
            [0, 'guardian'],
        ];

        for ($i = 0; $i < count($chunks) - 1; $i ++) {
            list ($value, $unit) = $chunks[$i];
            if ($value !== 0) {
                $text = pluralize($value, $unit);

                //append next unit as well, if it's available and nonzero
                list ($nextValue, $nextUnit) = $chunks[$i + 1];
                if ($nextValue !== 0) {
                    $text .= ' ' . pluralize($nextValue, $nextUnit);
                }

                return $text;
            }
        }
    }

    return 'just now';
}

和测试:

echo time_since(new DateTime('2014-01-01')) . PHP_EOL;
echo time_since(new DateTime('2014-10-09')) . PHP_EOL;
echo time_since(new DateTime('2014-11-09')) . PHP_EOL;
echo time_since(new DateTime('2014-11-10')) . PHP_EOL;
echo time_since(new DateTime('2014-11-10 13:05')) . PHP_EOL;
echo time_since(new DateTime('2114-11-10 13:05')) . PHP_EOL; //future

结果:

rr-@work:~$ php test.php
10 months 9 days
1 month 1 day
1 day 13 hours
13 hours 5 minutes
38 seconds
just now
于 2014-11-10T13:15:00.693 回答