Php 在Joomla中将日期转换为用户时区
谢谢你的阅读 只需要知道如何将从gmtime中的sql表中获取的datetime转换为用户时区中的datetime 以下是我的代码,但似乎不起作用Php 在Joomla中将日期转换为用户时区,php,datetime,joomla,time,timezone,Php,Datetime,Joomla,Time,Timezone,谢谢你的阅读 只需要知道如何将从gmtime中的sql表中获取的datetime转换为用户时区中的datetime 以下是我的代码,但似乎不起作用 //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 = J
//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;
}
它可以转换,但我从上面的代码中得到的时间都是错误的。这是适合我的函数:-
//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
}
这有点太离谱了,因为我希望使用JoomlaAPI中包含的函数来实现它。如果有人能提供更好的解决方案,请提供。我选择它作为正确答案。您没有指定Joomla版本,但是,您是否直接尝试了Joomla的JDate类
//获取用户及其时区
$user=JFactory::getUser();
$timeZone=$user->getParam('timeZone','UTC');
//在用户时区中创建设置为now的JDate对象。
$myDate=JDate::getInstance('now',$timeZone);
//获取作为UNIX时间戳的日期。
$myDate->toUnix():
//对于您的示例,请使用
函数转换用户的日期($date\U time)
{
//获取用户及其时区
$user=JFactory::getUser();
$timeZone=$user->getParam('timeZone','UTC');
//在用户时区中创建设置为now的JDate对象。
$myDate=JDate::getInstance($date\u time,$timeZone);
返回$myDate->toUnix();
}
最简单的方法是:
$useUserTimeZone = true;
JHtml::date($sqlGmtTimestamp , 'D F n, Y', $useUserTimeZone);
$sqlGmtTimestamp采用GMT时间戳/日期时间
$useUserTimeZone是使用用户时区的标志,否则将使用服务器时区
更多详情请点击此处:尝试以下操作:
$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
$date=JFactory::getDate();//现在-2014-03-11 08:45:22
$date->setOffset(8);//UTC+8
echo$date->toMySQL();//错-2014-03-11 08:45:22
回声“
”;
echo$date->toFormat();//右-2014-03-11 16:45:22
如果需要不同的格式,请使用第二个参数:
JHtml::date($post_arr[5],DateTime::RFC2822)
这相当于:
1.使用从数据库读取的UTC日期创建JDate对象
2.在Jomla全局配置和用户配置中获取正确的时区
3.调用setTimeZone()将JDate对象转换为用户本地时间
4.调用format()使用Joomla 2.5+(我认为)将JDate对象格式化为格式良好的字符串
echo JHtml::_('date', $input, $format, $tz, $gregorian);
$input可以是以下值之一:
- 当前时间的“现在”(默认值)
- 日期()接受格式的日期/时间字符串
- NULL以使用默认的基于区域设置的格式(默认)
- 日期格式规范字符串(请参阅)
- TRUE以使用用户的时区(默认)。注意:如果未设置用户的时区,则使用全局配置时区
- FALSE使用全局配置时区
- 空表示没有转换
- 时区字符串(例如:“美国/洛杉矶”,请参阅)
- 正确使用公历
- FALSE不使用公历(默认)
$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\u converted
以Y-m-d H:i:s
格式提供所需时区的日期。您绝对确定数据库中存储的时间是gmtime吗?你检查过几个值了吗?时区非常棘手。Joomla可能正在使用您的服务器时间,这可能不是GMT.Yes。我查过了。显示的计时是-6GMT,而不是我的用户时区设置,新加坡的时区设置应该是+8。在我的数据库中,我将计时格式输入为gmtime(“Y-m-d H:i:s”),当然必须有其他替代方案才能获得相同的结果,请检查此线程:
$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);