我将创建一个"未来"-博客(博客形式的科幻冒险),并希望显示所有日期+100年。例如,2012-05-17发布的帖子应该显示日期2112-05-17。
起初我以为我可以很容易地将日期设置为2112-05-17,但wordpress似乎无法处理高于2049的日期。
所以我的下一个想法是修改日期的显示方式。我想修改general-template.php中的get_the_date(),并使其在稍后返回日期。
但我的技术还不够。我对如何在php中使用日期值一无所知。
get_the_date()看起来像这样:
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);
}
关于如何修改它,有什么想法吗?那么它在归还之前又增加了100年?
任何输入都将被通知:)
看起来您可能需要调查date_modify和strtotime
http://php.net/manual/en/datetime.modify.php
http://www.php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/datetime.add.php
假设您的mysql日期为以下格式: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
}
现在您可以:
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);
}
尝试自定义字段:http://codex.wordpress.org/Custom_Fields
您必须为每个帖子输入+100年的日期,但这样您就不会依赖php或函数来更改当前日期。
WordPress提供了过滤器get_the_date
,允许在将值处理到主题或插件之前对其进行修改。
每次调用get_the_date()
时都会使用此筛选器。
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 ) );
}
此函数从帖子中获取post_date
,添加时间并根据get_the_date()
的格式或WordPress选项中配置的默认格式返回。