Ok, I've managed to make it, but I will need some help from the Bug Squad on testing this, because I have the feeling we have a bug here.
Here is how I solved it:
I saved the date as you suggested:
Code: Select all
//Assign the value of the date
//Date are store on UTC
$createdate = JFactory::getDate();
$createdate = $createdate->toMySQL();
and then displayed it this way:
Code: Select all
jimport('joomla.utilities.date');
$user =& JFactory::getUser();
if($user->get('id')) {
$tz = $user->getParam('timezone');
} else {
$conf =& JFactory::getConfig();
$tz = $conf->getValue('config.offset');
}
$jdate = new JDate($strdate);
$jdate->setOffset($tz);
$formatDate = $jdate->toFormat($format);
when I use the setOffset function and after that the format function the timeoffset is displayed correctly. This means +2h is really +2h and -2 to hours is -2 hours.
BUT!!! If I have this code
Code: Select all
jimport('joomla.utilities.date');
$user =& JFactory::getUser();
if($user->get('id')) {
$tz = $user->getParam('timezone');
} else {
$conf =& JFactory::getConfig();
$tz = $conf->getValue('config.offset');
}
$jdate = new JDate($strdate,$tz);
$formatDate = $jdate->toFormat($format);
then I get a wrong date.
my +2h offset becomes -2h offset.
I was looking at the toFormat function and we have this code
Code: Select all
function toFormat($format = '%Y-%m-%d %H:%M:%S')
{
$date = ($this->_date !== false) ? $this->_strftime($format, $this->_date + $this->_offset) : null;
return $date;
}
This explains to me the following thing.
If I just set $jdate = new JDate($strdate);
then I populate the $this->_date variable.
after that, when I use the setOffset function I populate the $this->_offset variable. That is why I get the correct value.
NOW.
Why when I create the data Object and I also give the timezone I get wrong time?
If I make this:
$jdate = new JDate($strdate,$tz); then I should get $this->_date variable, that has the offset added to it.
After that when I use the toFormat function, $this->_offset variable should be empty == 0(because I didn't set it) and strftime should return the right time.
I started looking at the constructor of the JDate class and I found that my code goes in this if statement:
Code: Select all
if (preg_match('~(\\d{4})-(\\d{2})-(\\d{2})[T\s](\\d{2}):(\\d{2}):(\\d{2})(.*)~', $date, $matches))
{
$this->_date = mktime(
$matches[4], $matches[5], $matches[6],
$matches[2], $matches[3], $matches[1]
);
if ($this->_date == false) {
return;
}
if (isset($matches[7][0])) {
if ($matches[7][0] == '+' || $matches[7][0] == '-') {
$tzOffset = 60 * (
substr($matches[7], 0, 3) * 60 + substr($matches[7], -2)
);
} elseif ($matches[7] == 'Z') {
$tzOffset = 0;
}
}
$this->_date -= $tzOffset;
return;
}
Do you see it?
$this->_date -= $tzOffset;
Why do we extract the timeOffset instead of adding it? Can someone explain this to me???