Advertisement

how to save date?

For Joomla! 1.5 Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general
Locked
User avatar
THE_AI
Joomla! Explorer
Joomla! Explorer
Posts: 252
Joined: Sat Jun 03, 2006 4:33 pm
Contact:

how to save date?

Post by THE_AI » Mon Apr 20, 2009 10:44 pm

Hey there!
I have on some configurations problem to show the right date with offset.

To store the date I use the normal mysql function now(). (in my query i have .... VALUES (now(),...

to show the formated date I use the following function:

Code: Select all

	function getLocalDate($strdate,$format='%Y-%m-%d %H:%M:%S')
	{
        $conf =& JFactory::getConfig();
        jimport('joomla.utilities.date');
        $jdate = new JDate($strdate, '');
        $formatDate = $jdate->toFormat($format);
        return $formatDate;
	}
When I change the offset, then the date value also changes, but on some configurations it is wrong. For example if there is an offset of 7h and we publish an article - the article date is:
Tuesday, 21 April 2009 05:15
Several minutes later we leave a comment and the comments date is:
2009-04-20 10:38:23

I really don't understand what the problem could be? Any help is appreciated!

Advertisement
ybong
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Wed Feb 11, 2009 3:32 am
Location: Malaysia

Re: how to save date?

Post by ybong » Thu Apr 23, 2009 5:10 am

Hi,

Could it your timezone difference problem or server timezone is different from your local time? Just a wild guess.

Regards,
YB
Regards,
YB

User avatar
THE_AI
Joomla! Explorer
Joomla! Explorer
Posts: 252
Joined: Sat Jun 03, 2006 4:33 pm
Contact:

Re: how to save date?

Post by THE_AI » Thu Apr 23, 2009 8:53 am

Hey there! Thank you for the reply. Now I see that this thing is not working on my PC too.

In global config I have timezone: 0 .
I posted an article and the time was set to 8:45AM
I posted a comment under this article one minute later and the date of the comment is 10:46 . (10:46 is the right time for my PC)

I believe that this has something to do with the mysql now() function that I use:
NOW()

Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.
http://dev.mysql.com/doc/refman/5.1/en/ ... nction_now

Do I understand this right? The mysql server adds the timezone alone? So basically it means that I will have to use the php date function, add the time zone, save it in the database and then the output should not add the timezone?

Is this the right way? Save the date with the timezone and then don't change the output?

User avatar
THE_AI
Joomla! Explorer
Joomla! Explorer
Posts: 252
Joined: Sat Jun 03, 2006 4:33 pm
Contact:

Re: how to save date?

Post by THE_AI » Thu Apr 23, 2009 9:08 am

Hm, I a little bit confused right now.
I don't use anymore the mysql now() function but the joomla's date one.
$datenow =& JFactory::getDate();
$datenow = $datenow->toMySQL();

If timezone is 0, then both the article and the comment have the same date.
When I change the timezone to +2, then the article gets +2 and the comment gets -2??????????????????
I try to output the date using this function:

Code: Select all

	function getLocalDate($strdate,$format='%Y-%m-%d %H:%M:%S')
	{
        $conf =& JFactory::getConfig();
        jimport('joomla.utilities.date');
        $jdate = new JDate($strdate,$conf->getValue('config.offset'));
        $formatDate = $jdate->toFormat($format);
        return $formatDate;
	}

when I make var_dump($conf->getValue('config.offset')) then the right timezone is shown +2 not -2.

ybong
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 237
Joined: Wed Feb 11, 2009 3:32 am
Location: Malaysia

Re: how to save date?

Post by ybong » Thu Apr 23, 2009 9:19 am

Hi THE_AI,

The following code is how I save my time and display it. Stored on UTC and display based on user's timezone.

Code: Select all


//Assign the value of the date
//Date are store on UTC
$createdate = JFactory::getDate();
$createdate = $createdate->toMySQL();

//Get User Timezone which is set when register a user
function getUserTimeZone()
{
	// get the user's time zone
	$user =& JFactory::getUser();
	$tz = $user->getParam('timezone');
		
	return $tz;
}

//Use for display the time according to user
function calcDateTime($dbTime, $userTimeZone)
{
	$dbTime = JFactory::getDate($dbTime);
	$sec =$dbTime->toUNIX();	//set the date time to second
	$userDateTime = gmdate("Y-m-d H:i:s", $sec + $userTimeZone * 3600); 
               //calculate the datetime base on timezone
			
	return $userDateTime;
}
Hope this help you.

Regards,
YB
Regards,
YB

User avatar
THE_AI
Joomla! Explorer
Joomla! Explorer
Posts: 252
Joined: Sat Jun 03, 2006 4:33 pm
Contact:

Re: how to save date?

Post by THE_AI » Thu May 28, 2009 4:42 pm

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???

User avatar
airton
Joomla! Ace
Joomla! Ace
Posts: 1368
Joined: Sun Nov 04, 2007 1:12 am
Location: Brazil
Contact:

Re: how to save date?

Post by airton » Mon Aug 01, 2011 12:08 am

THE_AI wrote:Why do we extract the timeOffset instead of adding it? Can someone explain this to me???
A few lines before to the ones you highlighted in your post, you'll see the following code:

Code: Select all

if ($date == 'now' || empty($date))
{
   $this->_date = strtotime(gmdate("M d Y H:i:s", time()));
   return;
}
As you can see, if you pass to the constructor an empty date or the string "now", it will use the time() function from PHP, whitch returns the server's local time. So, it thinks that the date passed is always the server's local time and, thus, extracts the time zone value from it to change it back to GMT/UTC.

Hope that clarifies things a little.

Regards,
Airton Torres
Joomla Bug Squad http://groups.google.com/group/joomlabugsquad
Community website - http://community.joomla.org
Unsolicited support PMs will be deleted and the user added to the foes list.

Advertisement

Locked

Return to “Joomla! 1.5 Coding”