2015-11-13 27 views
0

我正在開發一個WordPress的小部件,通過RSS源導入體育比分。 RSS提要項目有一個匹配日期和時間,所以它是非常重要的信息。在RSS提要看項目喜歡這樣:[星期六,2015年11月14日15:00:00 +0100],但是當我想這個日期在一個變量:WordPress的RSS訂閱獲取日期是UTC時間

$date = $item->get_date(); 

,我發現了以下輸出:2015年11月14日下午2:00

正確的時間應該是15:00(本地)。有人知道我如何從RSS提要中獲得正確的時間嗎?

$rss = fetch_feed('http://www.volleybal.nl/application/handlers/export.php?format=rss&type=vereniging&programma=7141&iRegionId=7000'); 

if (!is_wp_error($rss)) : // Checks that the object is created correctly 
    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity(); 

    // Build an array of all the items, starting with element 0 (first element). 
    $rss_items = $rss->get_items(0, $maxitems); 
endif; 

foreach ($rss_items as $item) : 
    $splitby = array('Wedstrijd:', 'Datum:', 'Speellocatie:'); 
    $text = esc_html($item->get_description()); 
    $split = explode(' ', $text); 
    $result = array(); 
    $temp = array(); 
    $date = strtotime($item->get_date()); 
    $day = date('j M', $date); 
    $time = date('G:i', $date); 
endforeach; 
+0

出於好奇,將是時區永遠是CET? – Ohgodwhy

+2

你正在獲得'正確的時間'。 '[星期六,2015年11月14日15:00:00 +0100]'是3pm + 1,又是4pm。基本上這個字符串是以UTC,+1表示的。你想要什麼時區? – Practically

+0

我看到我在解釋中犯了一個錯誤。該項目中的時間是15:00:00 +0100,當我用'$ item-> get_date()'得到日期時,我收到下午2:00。 – mikesteeghs

回答

0

我發現了一個解決方案:https://geek.hellyer.kiwi/2012/08/04/convert-utc-to-local-time-in-wordpress/

/* 
* Converts UTC time to local time as set in WordPress 
* Processes data in the format "Y-m-d H:i:s" (same format as used in WordPress core) 
* 
* @author Ryan Hellyer <[email protected]> 
*/ 
function convert_time($time) { 
$timestamp = strtotime($time); // Converting time to Unix timestamp 
$offset = get_option('gmt_offset') * 60 * 60; // Time offset in seconds 
$local_timestamp = $timestamp + $offset; 
$local_time = date_i18n('Y-m-d H:i:s', $local_timestamp); 
return $local_time; 
} 

$time = '2012-08-03 12:33:07'; 
echo convert_time($time);