PHP时间和未来的时间

时间:2015-05-18 12:59:21

标签: php time timeago

我有一个函数从时间戳中获取时间,我从互联网上的某个地方抓住它并开发和优化它。它功能齐全。这里的问题是它只能在前一段时间(从过去的时间戳)获得,它没有处理未来的时间戳(它返回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 );

}

&GT;

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 );
}