【问题标题】:Converting date to user timezone in Joomla在 Joomla 中将日期转换为用户时区
【发布时间】:2013-06-18 00:29:54
【问题描述】:

感谢阅读。

只需要知道我如何将从 gmtime 中的 sql 表中获取的日期时间转换为用户时区中的日期时间。

以下是我的代码,但似乎不起作用..

//WHERE $post_arr[5] is date from sql

$user_date=convert_date_for_user($post_arr[5]);

function convert_date_for_user($date_time){
    $user = JFactory::getUser();
    $db = JFactory::getDBO();

    $timezone=$user->getParam('timezone');

    echo $tz_offset;
    $user_date = JFactory::getDate($date_time,$timezone);
    $user_date_str = $user_date->toUnix(true);

    return $user_date_str;
}

它可以转换,但我从上面的代码中得到了所有错误的时间。

【问题讨论】:

  • 你确定数据库中存储的时间是gmtime吗?你检查过几个值吗?时区非常棘手。 Joomla 可能正在使用您的服务器时间,这可能不是 GMT。
  • 是的。我检查了。显示的时间是 -6 GMT,而不是我的用户时区设置,新加坡应该是 +8。在我的数据库中,我将时间格式输入为 gmtime("Y-m-d H:i:s")

标签: php datetime joomla time timezone


【解决方案1】:

最简单的方法:

$useUserTimeZone = true;
JHtml::date($sqlGmtTimestamp , 'D F n, Y', $useUserTimeZone);

$sqlGmtTimestamp 采用 GMT 时间戳/日期时间

$useUserTimeZone 是使用用户时区的标志,否则将使用服务器的时区。

更多详情:http://docs.joomla.org/API16:JHtml/date

【讨论】:

    【解决方案2】:

    您没有指定您的 Joomla 版本,但是您是否直接尝试过 Joomla 的 JDate 类?

    // Get the User and their timezone
    $user = JFactory::getUser();
    $timeZone = $user->getParam('timezone', 'UTC');
    
    // Create JDate object set to now in the users timezone.
    $myDate = JDate::getInstance('now', $timeZone);
    
    // Gets the date as UNIX time stamp.
    $myDate->toUnix():
    
    
    // For your example using a method
    function convert_date_for_user($date_time)
    {
        // Get the User and their timezone
        $user = JFactory::getUser();
        $timeZone = $user->getParam('timezone', 'UTC');
    
        // Create JDate object set to now in the users timezone.
        $myDate = JDate::getInstance($date_time, $timeZone);
    
        return $myDate->toUnix();
    }
    

    【讨论】:

      【解决方案3】:

      这是对我有用的功能:-

      //WHERE date_time is the format of the date taken directly from database(ie: 0000-00-00 00:00:00)
      function convert_time_zone($date_time){
          $user =& JFactory::getUser();
          $db = JFactory::getDBO();
          $timezone=$user->getParam('timezone','UTC');
      
          $time_object = new DateTime($date_time, new DateTimeZone('UTC'));
          $time_object->setTimezone(new DateTimeZone($timezone));
      
          $user_datetime=$time_object->format('Y-m-d H:i:s');
      
          //SELECT ONLY 1 line below
          return $user_datetime;  //WOULD RETURN DATETIME IN 0000-00-00 00:00:00  
          //OR
          return $time_object->getTimestamp();  //WOULD RETURN DATETIME IN UNIX TIMESTAMP
      }
      

      它有点偏僻,因为我希望使用 joomla API 中包含的函数来做到这一点。如果有人可以提供更好的解决方案,请这样做。我选择它作为正确答案。

      【讨论】:

        【解决方案4】:

        使用 Joomla 2.5+(我认为),您可以使用以下代码

        echo JHtml::_('date', $input, $format, $tz, $gregorian);
        

        $input 可以是以下值之一:

        • “现在”表示当前时间(默认)
        • date() 接受格式的日期/时间字符串

        $format 可以是以下值之一:

        $tz 可以是以下值之一:

        • TRUE 以使用用户的时区(默认)。注意:如果未设置用户的时区,则使用全局配置时区。
        • FALSE 使用全局配置时区
        • NULL 表示没有转换
        • 时区字符串(例如:“America/Los_Angeles”,参见http://php.net/manual/en/timezones.php)

        $gregorian 可以是以下值之一:

        • 正确使用公历
        • FALSE 表示不使用公历(默认)

        【讨论】:

          【解决方案5】:

          在这里尝试了所有给定的可能解决方案,但没有获得用户时区中的日期(Joomla!v.3.9.14),这是我的(经过验证) 解决方案:

          $oUser_TZ  = JFactory::getUser()->getTimezone();
          $aUser_tz  = (array)$oUser_TZ; // almost sure this step is not that necessary
          $full_date = JFactory::getDate('now', $aUser_tz['timezone']); // pretty sure $oUser_tz->timezone will work
          
          // I had try to use $full_date->Format('Y-m-d H:i:s') but it was giving me the non-converted-to-wanted-timezone date, so
          $date_converted = substr($full_date, 0, 19);
          

          date_converted 以Y-m-d H:i:s 格式为我提供所需时区的日期。

          【讨论】:

          【解决方案6】:

          试试这个:

            $date = JFactory::getDate(); // now - 2014-03-11 08:45:22
            $date->setOffset(8); // UTC+8
            echo $date->toMySQL();  // wrong - 2014-03-11 08:45:22
            echo '<br />';
            echo $date->toFormat(); // right - 2014-03-11 16:45:22
          

          【讨论】:

            【解决方案7】:
            JHtml::date($post_arr[5]);
            

            如果您想要不同的格式,请使用第二个参数: JHtml::date($post_arr[5], DateTime::RFC2822);

            相当于: 1. 使用从数据库中读取的 UTC 日期创建一个 JDate 对象 2.在Jomla全局配置和用户配置中获取正确的时区 3. 调用 setTimeZone() 将您的 JDate 对象转换为用户本地时间 4.调用format()将JDate对象格式化为格式化好的字符串

            【讨论】:

              猜你喜欢
              • 2013-10-21
              • 1970-01-01
              • 2013-08-20
              • 1970-01-01
              • 1970-01-01
              • 2019-09-15
              • 1970-01-01
              • 2016-02-17
              • 2012-01-30
              相关资源
              最近更新 更多