选择语言 :

 Core_Date::span

Returns time difference between two timestamps, in human readable format. If the second timestamp is not given, the current time will be used. Also consider using [Date::fuzzy_span] when displaying a span.

$span = Date::span(60, 182, 'minutes,seconds'); // array('minutes' => 2, 'seconds' => 2) $span = Date::span(60, 182, 'minutes'); // 2

string Core_Date::span( integer $remote [, integer $local = null , string $output = string(45) "years,months,weeks,days,hours,minutes,seconds" ] )

参数列表

参数 类型 描述 默认值
$remote integer Timestamp to find the span of
$local integer Timestamp to use as the baseline null
$output string Formatting string string(45) "years,months,weeks,days,hours,minutes,seconds"
返回值
  • string when only a single output is requested
  • array associative list of all outputs requested
File: ./core/classes/date.class.php
public static function span($remote, $local = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds')
{
    // Normalize output
    $output = trim(strtolower((string)$output));

    if ( !$output )
    {
        // Invalid output
        return FALSE;
    }

    // Array with the output formats
    $output = preg_split('/[^a-z]+/', $output);

    // Convert the list of outputs to an associative array
    $output = array_combine($output, array_fill(0, count($output), 0));

    // Make the output values into keys
    extract(array_flip($output), EXTR_SKIP);

    if ( $local === NULL )
    {
        // Calculate the span from the current time
        $local = time();
    }

    // Calculate timespan (seconds)
    $timespan = abs($remote - $local);

    if ( isset($output['years']) )
    {
        $timespan -= Date::YEAR * ($output['years'] = (int)floor($timespan / Date::YEAR));
    }

    if ( isset($output['months']) )
    {
        $timespan -= Date::MONTH * ($output['months'] = (int)floor($timespan / Date::MONTH));
    }

    if ( isset($output['weeks']) )
    {
        $timespan -= Date::WEEK * ($output['weeks'] = (int)floor($timespan / Date::WEEK));
    }

    if ( isset($output['days']) )
    {
        $timespan -= Date::DAY * ($output['days'] = (int)floor($timespan / Date::DAY));
    }

    if ( isset($output['hours']) )
    {
        $timespan -= Date::HOUR * ($output['hours'] = (int)floor($timespan / Date::HOUR));
    }

    if ( isset($output['minutes']) )
    {
        $timespan -= Date::MINUTE * ($output['minutes'] = (int)floor($timespan / Date::MINUTE));
    }

    // Seconds ago, 1
    if ( isset($output['seconds']) )
    {
        $output['seconds'] = $timespan;
    }

    if ( count($output) === 1 )
    {
        // Only a single output was requested, return it
        return array_pop($output);
    }

    // Return array
    return $output;
}