2012-09-28 47 views
3

我已經創建了,我提出的自定義組件的代碼:的Joomla 2.5顯示錯誤的日期時間

$date = date('m/d/Y h:i:s a', time())."<br>"; 
echo 'Current date and time is: ' . $date; 

$date = JFactory::getDate(); 
echo 'Current date and time is: ' . $date->toFormat() ."<br>"; 

第一代碼正確顯示日期時間,但第二個顯示時間3小時

我檢查了configuration.php文件和public $ offset ='Europe/Athens';並且是正確的。我也在更改系統配置菜單中的設置,但似乎沒有修復JFactory :: getDate()以顯示正確的時間。 我錯過了什麼?

回答

8

對於在JFactory::getdate()你的第二個參數 - 我想你應該可以指定時間段的第二個參數所以像JFactory::getDate($time=now, $tzOffset)

$date = JFactory::getDate($input='now', 'UTC'); 
// Set the correct time zone based on the server configuration. 
$config = JFactory::getConfig(); 
$date->setOffset($config->getValue('config.offset')); 
//Print out Date 
echo $date->toFormat(); 

在一個側面說明它可能是更容易使用JHTML ::日期(),因爲這涉及到更少的行,並且更加'Joomla本地'。請參閱此here上的API頁面。然後使用如下代碼:

echo JHtml::date($input = 'now', 'm/d/Y h:i:s a', false); 

其中$input = now指定使用時間'now'。第二個參數是日期的格式,第三個參數表示時間設置設置爲服務器時間。而不是用戶選擇的時間。

+0

yes it works thanx – themis

+0

這是一個很好的答案,花了我一段時間才找到它,但這是Joomla文檔給你的;-) –