Every time I save the record the created time is updated with two hours, but why?

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
nkamp
Joomla! Apprentice
Joomla! Apprentice
Posts: 47
Joined: Wed Mar 08, 2006 7:20 am

Every time I save the record the created time is updated with two hours, but why?

Post by nkamp » Sun Jun 13, 2021 7:46 pm

Hello,
I’m busy with developing an component. I,m talking about backend. The created time is only saved the first time! But if I save the/update the record, the created time is updated with adding 2 hours.

First save: Site value: 2021-06-13 20:26:02; database value: 2021-06-13 18:26:02
Value’s after Second save: Site value: 2021-06-13 22:26:02; database value: 2021-06-13 20:26:02
Value’s after third save: Site value: 2021-06-14 00:26:02; database value: 2021-06-13 22:26:02
Value’s after fourth save. Site value: 2021-06-14 02:26:02; database value: 2021-06-14 00:26:02

As you can see, every time after save, two hours are added to the time.
What is happening I think is that after every save, the database value 2 hours are added (why only the hours?), this new value shown in the detail page (edit.php). And so fort, next save is this repeated.

The Joomla language is English, Apache is Europe/Amsterdam.
The created time is not complete updated, because minutes and seconds are still the same.

Has anybody an idea why this happening?

Nico

User avatar
AMurray
Joomla! Exemplar
Joomla! Exemplar
Posts: 9711
Joined: Sat Feb 13, 2010 7:35 am
Location: Australia

Re: Every time I save the record the created time is updated with two hours, but why?

Post by AMurray » Sun Jun 13, 2021 10:05 pm

I believe the discrepancy is the web server /data centre location (not your local PC's time/location) vs GMT.

In this case the difference between Amsterdam/Europe and Greenwich (London) GMT just happens to be +2:00 hours. This would explain the 'minutes' being the same.....so 2:06am + 2 hours is 4:06am.

If you were two hours behind GMT, I would expect to see the time difference being *earlier* by two hours.

I don't know if it would make a difference but here's something to try; set the time zone to something that equals what you think it should be. Set it to GMT for example - does it still add the two hours? Set it to four hours behind GMT - do you see the equivalent time difference?
Regards - A Murray
General Support Moderator

nkamp
Joomla! Apprentice
Joomla! Apprentice
Posts: 47
Joined: Wed Mar 08, 2006 7:20 am

Re: Every time I save the record the created time is updated with two hours, but why?

Post by nkamp » Mon Jun 14, 2021 8:59 pm

I'm a little bit further but still not working.
I compare it with the com_content and I have defined the field exactly the same:

Code: Select all

<field  name="created_time" 
                type="calendar"
                label="JGLOBAL_FIELD_CREATED_LABEL" 
                description="JGLOBAL_FIELD_CREATED_DESC"
                translateformat="true"
		showtime="true"
                size="22" 
        />
except the name is slightly different 'created_time' instead off 'created'.

php.ini: date.timezone=Europe/Amsterdam (Development laptop with apache installed).
Joomla: Global configuration --> Server --> Local settings: Amsterdam

Than I test it with article, which working for the created date:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ database value _ | detail page value (edit.php) | The real time
1: I leaf the created field empty: 2021-06-14 19:20:23 | 2021-06-14 21:20:23 | 2021-06-14 19:20:23
2: Calendar selected date/time : 2021-06-14 19:22:24 | 2021-06-14 21:22:24 | not important, it is selected datetime

So this is working for article, for the screen it is corrected, and the user see the right time.

But if I do the same with my component than the result is different:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ database value _ | detail page value (edit.php) | The real time
1: I leaf the created field empty: 0000-00-00 00:00:00 | empty, it's not good but I can understand |
2: Calendar selected date/time : 2021-06-14 21:27:12 | 2021-06-14 21:27:12 | 2021-06-14 21:27:12

For me now, this means that there is something in the article which is doing:
  1. If the user doesn't select a datetime of the calendar field, the datetime is filled in by the com_content but were?
  2. If the user select a datetime at the calendar, an corrected time is saved in the database, and the right value is shown at the edit.php. But how? Were is this done
  3. I thought it was over but it is still doing, If I save the record again, the hours of my component record is everytime increased with two hours. But if I set in Joomla global configuration --> server --> Local time settings from Amsterdam to Universal Coördinated time, than is this over. How does this work?
What do I not see or understand?

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 44071
Joined: Sat Apr 05, 2008 9:58 pm

Re: Every time I save the record the created time is updated with two hours, but why?

Post by Webdongle » Tue Jun 15, 2021 9:24 am

afaik
The created date/time is taken from the date/time on the device used to create the Article
Once created the date/time does not change unless manually changed or an extension changes it.

Where is your component taking the date/time from? The server?
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"When I'm right no one remembers but when I'm wrong no one forgets".

nkamp
Joomla! Apprentice
Joomla! Apprentice
Posts: 47
Joined: Wed Mar 08, 2006 7:20 am

Re: Every time I save the record the created time is updated with two hours, but why?

Post by nkamp » Tue Jun 15, 2021 3:02 pm

Hello Afaik,

Thanks. I don't know what you mean by 'device' but I'm developing my component on my windows 10 laptop. And I have installed Apache/PHP7.4/MariaDB. (Not XAMPP)
Than the only device is than my laptop I suppose.

But for creating the article it is working fine!! No problems with it, the time is correct. That's I think the remarkable/strange thing or..., and I can't find in the code for the com_content (article0 if there is done something special.

For my component I have at nothing done special. I have search through all components also the Joomla components and I have found this in com_finder, I have tried this:

Code: Select all

public function store($updateNulls = false) {

        $date   = JFactory::getDate()->toSql();
        $userId = JFactory::getUser()->id;
        
        $this->modified_time    = $date;
        $this->modified_by      = $userId;

         if (!(int) $this->created_time) {
               $this->created_time = $date; // This is the line adding every save two hours.
         }

         if (empty($this->created_by)) {
             $this->created_by = $userId;
         }

        // Attempt to store the data.
        return parent::store($updateNulls);
    }
But if leave this code out of the store function, nothing is stored in the database for these four fields.

I expect from the framework that these fields are populated by the framework. As you do not fill in an created time at the article, at saving an created time is filled in and written in the database. But not for my component!!!???

Were do I'am going wrong or what do I not understand.

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 44071
Joined: Sat Apr 05, 2008 9:58 pm

Re: Every time I save the record the created time is updated with two hours, but why?

Post by Webdongle » Tue Jun 15, 2021 5:26 pm

Does your component create it's own database tables to store its information?
Does your component get the date from the device that is using it (i.e. your laptop if you are using the component. or tablet/pc etc. if an admin logs in from a different device.

Your component needs its own tables to store the date/time in
Your component needs to fetch the date/time from the device that is creating with it. All be it your laptop or the pc etc. of any admin likely to use that component in the future.
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"When I'm right no one remembers but when I'm wrong no one forgets".

nkamp
Joomla! Apprentice
Joomla! Apprentice
Posts: 47
Joined: Wed Mar 08, 2006 7:20 am

Re: Every time I save the record the created time is updated with two hours, but why?

Post by nkamp » Tue Jun 22, 2021 3:14 pm

Hello,

I understand now my issue, but not how to solve it right now. It's due the timezone.

If I'm looking at the com_content (article) then the created time is saved to the database like:

the real time is: 2021-06-17 19:48:56
time saved in database: 2021-06-17 17:48:56
time presented detail page: 2021-06-17 19:48:56

I understand this and I want to follow up this principle.

For my component development, the first save is working with:

Code: Select all

$this->created_time = $date->toSql();
, the value's are as the date value's saved for an article.

But after every update it goes wrong. After every update save, two hours are added.

This is due that the presented time, as here: 2021-06-17 19:48:56, is send to the save function and not corrected with UTC time. The value '2021-06-17 19:48:56' is saved in the database and now '2021-06-17 21:48:56" is presented on the detail page.
I expected that this should be corrected by the Joomla framework! So I have made my own save functions in the controller/model and before the parent::save is called I should correct the date-time with utc time like:

Code: Select all

$data['created_time'] = ? //= corrected UTC time, which should be saved
if (!(parent::save($data))) {
            return false;
} 
I have looked at https://docs.joomla.org/How_to_use_JDate, but there is not an good code example.

I have been debugging saving of an article, but can't find clearly find what is needed to correct the time with USER_UTC.

Does somebody have an idea how to do this, or know an code example how this can be solved?

EDIT:
BTW: I'm now reading this: "... JForm calender time you can provide a filter: SERVER_UTC or USER_UTC that will handle the timezone calculations for you...". I'm using calender and filter SERVER_UTC as with the article, but time is not corrected in the way as the com_content/article does! Or do I something else wrong?


Locked

Return to “Joomla! 3.x Coding”