使用PHP的博客文章的时间格式

时间:2017-06-12 01:38:13

标签: php datetime time

我正在尝试以类似的格式显示时间:asset-urlfew sec ago5 min ago等。我搜索并查找了一些代码,并使其可行。

问题

它没有显示正确的结果。当我发帖时,它显示了正确的"现在"如果已经2分钟等等。

"代码" `

2 days ago

>`

使用

define( "TIMEBEFORE_NOW",         'now' );
define( "TIMEBEFORE_MINUTE",      '{num} minute ago' );
define( "TIMEBEFORE_MINUTES",     '{num} minutes ago' );
define( "TIMEBEFORE_HOUR",        '{num} hour ago' );
define( "TIMEBEFORE_HOURS",       '{num} hours ago' );
define( "TIMEBEFORE_YESTERDAY",   'yesterday' );
define( "TIMEBEFORE_FORMAT",      '%e %b' );
define( "TIMEBEFORE_FORMAT_YEAR", '%e %b, %Y' );

function time_ago( $time )
{
    $out    = ''; // what we will print out
    $now    = time(); // current time
    $diff   = $now - strtotime($time); // difference between the current and the provided dates

    if( $diff < 60 ) // it happened now
        return TIMEBEFORE_NOW;

    elseif( $diff < 3600 ) // it happened X minutes ago
        return str_replace( '{num}', ( $out = round( $diff / 60 ) ), $out == 1 ? TIMEBEFORE_MINUTE : TIMEBEFORE_MINUTES );

    elseif( $diff < 3600 * 24 ) // it happened X hours ago
        return str_replace( '{num}', ( $out = round( $diff / 3600 ) ), $out == 1 ? TIMEBEFORE_HOUR : TIMEBEFORE_HOURS );

    elseif( $diff < 3600 * 24 * 2 ) // it happened yesterday
        return TIMEBEFORE_YESTERDAY;

    else // falling back on a usual date format as it happened later than yesterday
        return strftime( date( 'Y', strtotime($time)) == date( 'Y' ) ? TIMEBEFORE_FORMAT : TIMEBEFORE_FORMAT_YEAR, strtotime($time));
}

任何帮助将不胜感激。

0 个答案:

没有答案