0

我有一个从时间戳获取时间前的功能,我从互联网的某个地方抓取它并开发和优化它。它功能齐全。这里的问题是它只获取时间之前(来自过去的时间戳),它没有处理未来的时间戳(它返回 0 秒前)。有人能帮我把这个功能添加到函数中吗?

<?php

function time_ago( $ts, $format ) {

    // $format = 'l, F d, Y H:i';

    $granularity    = 1;

    $dif            = time() - $ts;

    if ( $dif < 0 )

        return '0 Seconds ago';

    elseif ( $dif < 604800 ) { // 604800 7 days / 864000 10 days

        $periods = array(

            'Week'      => 604800,
            'Day'       => 86400,
            'Hour'      => 3600,
            'Minute'    => 60,
            'Second'    => 1

        );

        $output = '';

        foreach ( $periods as $key => $value ) {

            if ( $dif >= $value ) {

                $time = round( $dif / $value );

                $dif %= $value;

                $output .= ( $output ? ' ' : '' ) . $time . ' ';

                $output .= ( ( $time > 1 ) ? $key . 's' : $key );

                $granularity --;

            }

            if ( $granularity == 0 )

                break;

        } // foreach( $periods as $key => $value )

        return ($output ? $output : '0 seconds') . ' ago';

    } else

        return date( $format, $ts );

}

?>

4

2 回答 2

1

这应该工作

<?php
function diffPeriods($diff)
{
    $periods = array(
        'Week' => 604800,
        'Day' => 86400,
        'Hour' => 3600,
        'Minute' => 60,
        'Second' => 1
    );

    $granularity = 1;

    $output = '';
    foreach ($periods as $key => $value) {

        if ($diff >= $value) {

            $time = round($diff / $value);

            $diff %= $value;

            $output .= ($output ? ' ' : '') . $time . ' ';
            $output .= (($time > 1) ? $key . 's' : $key);

            $granularity --;
        }

        if ($granularity == 0) {
            break;
        }
    }

    return $output;
}

function time_ago($ts, $format)
{
    $diff = time() - $ts;

    if ($diff == 0) {
        return 'Now';
    }

    if ($diff < 604800 && $diff > 0) {
        // 604800 7 days / 864000 10 days

        $periods = array(
            'Week' => 604800,
            'Day' => 86400,
            'Hour' => 3600,
            'Minute' => 60,
            'Second' => 1
        );

        $output = diffPeriods($diff);

        return ($output ? $output : '0 seconds') . ' ago';
    } elseif ($diff < 0 && $diff > - 604800) {
        $output = diffPeriods($diff * - 1);

        return 'in ' . ($output ? $output : '0 seconds');
    }

    // too old/new...display the date
    return date($format, $ts);
}

var_dump(time_ago(time(), 'l, F d, Y H:i')); // now
var_dump(time_ago(time() + 3600, 'l, F d, Y H:i')); // 1 hour in the future
var_dump(time_ago(time() + (3600 * 50), 'l, F d, Y H:i')); // 2 daysin the future
var_dump(time_ago(time() + (3600 * 24 * 11), 'l, F d, Y H:i')); // 11 days in the future
var_dump(time_ago(time() - 3600, 'l, F d, Y H:i')); // 1 hour ago
var_dump(time_ago(time() - (3600 * 50), 'l, F d, Y H:i')); // 2 days ago
var_dump(time_ago(time() - (3600 * 24 * 11), 'l, F d, Y H:i')); // 11 days ago
于 2015-05-18T13:19:56.987 回答
1

我已经编辑了你的功能。现在它将未来的时间显示为In 10 Hours

代码:

function time_ago( $ts, $format ) {

    // $format = 'l, F d, Y H:i';
    $granularity    = 1;
    $dif            = time() - $ts;
    $future = $dif < 0 ? true : false;
    $dif = abs($dif);
    if ( $dif < 604800 ) { // 604800 7 days / 864000 10 days
        $periods = array(
            'Week'      => 604800,
            'Day'       => 86400,
            'Hour'      => 3600,
            'Minute'    => 60,
            'Second'    => 1
        );
        $output = '';
        foreach ( $periods as $key => $value ) {
            if ( $dif >= $value ) {
                $time = round( $dif / $value );
                $dif %= $value;
                $output .= ( $output ? ' ' : '' ) . $time . ' ';
                $output .= ( ( $time > 1 ) ? $key . 's' : $key );
                $granularity --;
            }
            if ( $granularity == 0 )
            break;
        } // foreach( $periods as $key => $value )
        if($future) {
            return "In " . ($output ? $output : '0 seconds');
        } else {
            return ($output ? $output : '0 seconds') . ' ago';
        }
    } else
    return date( $format, $ts );
}
于 2015-05-18T13:20:00.470 回答