2012-03-05 33 views
0

我要創建一個「未來」-blogg(博客形式的科幻冒險),並希望顯示所有日期+100年。例如2012-05-17發佈的帖子應顯示日期2112-05-17。我想在顯示的wordpress日期中添加100年

首先,我想我可能只是輕鬆地將日期設置爲2112年5月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年?

任何投入將appriciated :)

+7

'我不知道如何使用php中的日期值處理任何事情。然後,閱讀手冊和PHP書籍的時間。 – 2012-03-05 14:37:26

回答

0

假設你的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); 
} 
+0

'$分鐘'實際上是秒,所以你有一個令人誤解的變量名。該功能的其餘部分也很麻煩。只需在日期的字符串表示中使用'strtotime()'。 – MetalFrog 2012-03-05 15:00:11

+0

感謝隊友,補丁;-)另外,strtotime()在某些舊版本的php上表現略有不同 - 我對於沒有運行最新和最好的客戶端有一些不好的體驗 – 2012-03-05 15:07:03

+0

這裏是一個完美的例子今天)我正在談論與strtotime()http:// stackoverflow。com/questions/9656436/strtotime-weird-behavior – 2012-03-11 15:55:16

0

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的選項來配置的默認格式的格式。