我在我制作的自定义组件中创建了此代码:
$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>";
Run Code Online (Sandbox Code Playgroud)
第一个代码正确显示日期时间,但第二个代码显示时间+3小时
我检查了configuration.php文件和public $ offset ='Europe/Athens'; 并且是正确的.我也在更改系统配置菜单中的设置,但似乎没有修复JFactory :: getDate()来显示正确的时间.我错过了什么?
对于你的第二个参数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();
Run Code Online (Sandbox Code Playgroud)
另外,在组件中使用JHtml :: date()可能更容易,因为这涉及更少的行并且更多是"Joomla native".请参阅此处的API页面.然后使用如下代码:
echo JHtml::date($input = 'now', 'm/d/Y h:i:s a', false);
Run Code Online (Sandbox Code Playgroud)
Where $input= now指定使用'now'时间.第二个参数是日期的格式,第三个参数表示时间设置是设置服务器时间.而不是用户选择的时间.