【问题标题】:Joomla 2.5 displays wrong datetimeJoomla 2.5 显示错误的日期时间
【发布时间】:2012-09-20 04:19:10
【问题描述】:

我在自己制作的自定义组件中创建了这段代码:

$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() 以显示正确的时间。 我错过了什么?

【问题讨论】:

    标签: joomla joomla2.5


    【解决方案1】:

    对于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 原生”。请参阅此 here 上的 API 页面。然后使用如下代码:

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

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

    【讨论】:

    • 这是一个很好的答案,我花了一段时间才找到它,但这是适合你的 Joomla 文档 ;-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 1970-01-01
    相关资源
    最近更新 更多