php 过滤the_time和get_the_date以返回相对字符串

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 过滤the_time和get_the_date以返回相对字符串相关的知识,希望对你有一定的参考价值。

/**
 * Filter the_time and get_the_date to return a relative string.
 * Inspired by http://www.jasonbobich.com/2011/04/10/a-better-way-to-add-time-ago-to-your-wordpress-theme/
 * Based on code from https://buddypress.trac.wordpress.org/browser/tags/2.8.2/src/bp-core/bp-core-functions.php#L1118
 *
 * @param  string $formatted The formatted time.
 * @param  string $format    The time format used.
 * @param  object $the_post  Optional - current Post object.
 * @return string            Modified formatted time.
 */
function cc_relative_time( $formatted, $format, $the_post = false ) {

    // Certain formats are likely to mean use in <time> elements and similar so don't touch them.
    $timestamp_formats = array(
        'c', // ISO 8601.
        'r', // RFC 2822.
        'U', // Unix timestamp.
    );
    if ( in_array( $format, $timestamp_formats, true ) ) {
        return $formatted;
    }

    // Get the current post object from the global if not supplied.
    if ( ! $the_post ) {
        global $post;
        $the_post = $post;
    }
    // Get the date/time for the post in GMT.
    $post_date = get_post_time( 'U', true, $the_post );

    // Get the current data/time in GMT for comparrison.
    $current_date = current_time( 'timestamp', true );

    // Language vars.
    $unknown_text = __( 'sometime', 'customcreative' );
    $right_now_text = __( 'right now', 'customcreative' );
    $ago_text = __( '%s ago', 'customcreative' );

    // Array of time period chunks.
    $chunks = array(
        YEAR_IN_SECONDS,
        30 * DAY_IN_SECONDS,
        WEEK_IN_SECONDS,
        DAY_IN_SECONDS,
        HOUR_IN_SECONDS,
        MINUTE_IN_SECONDS,
        1,
    );

    // Difference in seconds between curent time and post date/time.
    $since = $current_date - $post_date;

    // Something went wrong with date calculation and we ended up with a negative date.
    if ( 0 > $since ) {
        return $formatted;
    }

    // Find the relevant time chunk.
    for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) {
        $seconds = $chunks[ $i ];
        // Finding the biggest chunk (if the chunk fits, break).
        $count = intval( $since / $seconds );
        if ( 0 !== $count ) {
            break;
        }
    }

    // If $i iterates all the way to $j, then the event happened 0 seconds ago.
    if ( ! isset( $chunks[ $i ] ) ) {
        $output = $right_now_text;
    } else {
        // Set output var based on $chunk.
        switch ( $seconds ) {
            case YEAR_IN_SECONDS :
                $output = sprintf( _n( '%s year ago', '%s years ago', $count, 'customcreative' ), $count );
                break;
            case 30 * DAY_IN_SECONDS :
                $output = sprintf( _n( '%s month ago', '%s months ago', $count, 'customcreative' ), $count );
                break;
            case WEEK_IN_SECONDS :
                $output = sprintf( _n( '%s week ago', '%s weeks ago', $count, 'customcreative' ), $count );
                break;
            case DAY_IN_SECONDS :
                $output = sprintf( _n( '%s day ago', '%s days ago', $count, 'customcreative' ), $count );
                break;
            case HOUR_IN_SECONDS :
                $output = sprintf( _n( '%s hour ago', '%s hours ago', $count, 'customcreative' ), $count );
                break;
            case MINUTE_IN_SECONDS :
                $output = sprintf( _n( '%s minute ago', '%s minutes ago', $count, 'customcreative' ), $count );
                break;
            default:
                $output = sprintf( _n( '%s second ago', '%s seconds ago', $count, 'customcreative' ), $count );
        }
    }

    return $output;
}
add_filter( 'the_time', 'cc_relative_time', 10, 2 );
add_filter( 'get_the_time', 'cc_relative_time', 10, 3 );
add_filter( 'the_date', 'cc_relative_time', 10, 2 );
add_filter( 'get_the_date', 'cc_relative_time', 10, 3 );

以上是关于php 过滤the_time和get_the_date以返回相对字符串的主要内容,如果未能解决你的问题,请参考以下文章

php计算几分钟前几小时前几天前的几个函数

PHP 过滤器

php 过滤和验证PHP

用php过滤html部分标签

(过滤和转义)php自带过滤和转义函数

php #laravel #php用于排序和搜索/过滤的查询范围