Joomla 2.5显示错误的日期时间

时间:2012-09-28 09:30:56

标签: joomla joomla2.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()来显示正确的时间。 我错过了什么?

1 个答案:

答案 0 :(得分: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 :: date()可能更容易,因为这涉及更少的行并且更多是“Joomla native”。请参阅此here上的API页面。然后使用如下代码:

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

$input =现在指定使用“现在”时间。第二个参数是日期的格式,第三个参数表示时间设置是设置服务器时间。而不是用户选择的时间。

相关问题