选择语言 :

 Core_Date::fuzzy_span

Returns the difference between a time and now in a "fuzzy" way. Displaying a fuzzy time instead of a date is usually faster to read and understand.

$span = Date::fuzzy_span(time() - 10); // "moments ago" $span = Date::fuzzy_span(time() + 20); // "in moments"

A second parameter is available to manually set the "local" timestamp, however this parameter shouldn't be needed in normal usage and is only included for unit tests

string Core_Date::fuzzy_span( integer $timestamp [, integer $local_timestamp = null ] )

参数列表

参数 类型 描述 默认值
$timestamp integer "remote" timestamp
$local_timestamp integer "local" timestamp, defaults to time() null
返回值
  • string
File: ./core/classes/date.class.php
public static function fuzzy_span($timestamp, $local_timestamp = NULL)
{
    $local_timestamp = ($local_timestamp === NULL) ? time() : (int)$local_timestamp;

    // Determine the difference in seconds
    $offset = abs($local_timestamp - $timestamp);

    if ( $offset <= Date::MINUTE )
    {
        $span = 'moments';
    }
    elseif ( $offset < (Date::MINUTE * 20) )
    {
        $span = 'a few minutes';
    }
    elseif ( $offset < Date::HOUR )
    {
        $span = 'less than an hour';
    }
    elseif ( $offset < (Date::HOUR * 4) )
    {
        $span = 'a couple of hours';
    }
    elseif ( $offset < Date::DAY )
    {
        $span = 'less than a day';
    }
    elseif ( $offset < (Date::DAY * 2) )
    {
        $span = 'about a day';
    }
    elseif ( $offset < (Date::DAY * 4) )
    {
        $span = 'a couple of days';
    }
    elseif ( $offset < Date::WEEK )
    {
        $span = 'less than a week';
    }
    elseif ( $offset < (Date::WEEK * 2) )
    {
        $span = 'about a week';
    }
    elseif ( $offset < Date::MONTH )
    {
        $span = 'less than a month';
    }
    elseif ( $offset < (Date::MONTH * 2) )
    {
        $span = 'about a month';
    }
    elseif ( $offset < (Date::MONTH * 4) )
    {
        $span = 'a couple of months';
    }
    elseif ( $offset < Date::YEAR )
    {
        $span = 'less than a year';
    }
    elseif ( $offset < (Date::YEAR * 2) )
    {
        $span = 'about a year';
    }
    elseif ( $offset < (Date::YEAR * 4) )
    {
        $span = 'a couple of years';
    }
    elseif ( $offset < (Date::YEAR * 8) )
    {
        $span = 'a few years';
    }
    elseif ( $offset < (Date::YEAR * 12) )
    {
        $span = 'about a decade';
    }
    elseif ( $offset < (Date::YEAR * 24) )
    {
        $span = 'a couple of decades';
    }
    elseif ( $offset < (Date::YEAR * 64) )
    {
        $span = 'several decades';
    }
    else
    {
        $span = 'a long time';
    }

    if ( $timestamp <= $local_timestamp )
    {
        // This is in the past
        return $span . ' ago';
    }
    else
    {
        // This in the future
        return 'in ' . $span;
    }
}