I want to add 100 years to the displayed wordpress

2019-08-19 13:33发布

I'm going to create a "future"-blogg (sort of a sci-fi-adventure in blog form) and want to display all dates +100 years. For instance a post published 2012-05-17 should display the date 2112-05-17.

First I thought I could just easily set the date to 2112-05-17, but it seems that wordpress can't handle dates higher than 2049.

So my next idea is to modify how the dates are displayed. I was thinking of modifying get_the_date() in general-template.php, and make it return the later date.

But here my skills are not enough. I don't know anything about how to work with date values in php.

get_the_date() looks like this:

function get_the_date( $d = '' ) {
        global $post;
        $the_date = '';

        if ( '' == $d )
                $the_date .= mysql2date(get_option('date_format'), $post->post_date);
        else
                $the_date .= mysql2date($d, $post->post_date);

        return apply_filters('get_the_date', $the_date, $d);
}

Any ideas on how to modify it? So it adds 100 years to the date before returning it?

Any input would be appriciated :)

标签: php wordpress
4条回答
女痞
2楼-- · 2019-08-19 14:05

WordPress provides the filter get_the_date that allows to modify the value before it is handled over to the theme or plugin.

This filter is used everytime get_the_date() is called.

add_filter( 'get_the_date', 'modify_get_the_date', 10, 3 );
function modify_get_the_date( $value, $format, $post ) {
    $date = new DateTime( $post->post_date );
    $date->modify( "+100 years" );
    if ( $format == "" )
        $format = get_option( "date_format" );
    return( $date->format( $format ) );
}

This function takes the post_date from the post, adds the time and returns it according to the format given to get_the_date() or with the default format configured in the WordPress options.

查看更多
狗以群分
4楼-- · 2019-08-19 14:17

Try a custom field: http://codex.wordpress.org/Custom_Fields

You will have to enter the +100 year date for each post, but then you're not going to be relying on php or functions to alter the current date.

查看更多
不美不萌又怎样
5楼-- · 2019-08-19 14:24

Assuming your mysql dates are of the following format: YYYY-MM-DD

function add100yr( $date="2011-03-04" ) {
    $timezone=date_timezone_get();
    date_default_timezone_set($timezone);

    list($year, $month, $day) = split(':', $date); 
    $timestamp=mktime(0,0,0, $month, $day, $year);

    // 100 years, 365.25 days/yr, 24h/day, 60min/h, 60sec/min
    $seconds = 100 * 365.25 * 24 * 60 * 60;
    $newdate =  date("Y-m-d", $timestamp+$seconds );
    // $newdate is now formatted YYYY-mm-dd
}

Now you can:

function get_the_date( $d = '' ) {
    global $post;
    $the_date = '';

    if ( '' == $d )
            $the_date .= mysql2date(get_option('date_format'), add100yr($post->post_date));
    else
            $the_date .= mysql2date($d, add100yr($post->post_date));

    return apply_filters('get_the_date', $the_date, $d);
}
查看更多
登录 后发表回答