Timestamp issue

Hello Experts,

I have implemented the below timestamp behaviour. Its "created_at" time is working fine. But when i am updating the page the "updated_at" is showing the same time as of "created_at" time.

[b]

Can you help me how to get the "updated_at" time/where am I making mistake??[/b]

One more thing can I get the time difference between "created_at" and "updated_at" time??




 public function behaviors()

	    {

	        return [

	            [

	                'class' => TimestampBehavior::className(),

	                'attributes' => [

	                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],

	                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],

	                ],

	                'value' => new Expression('NOW()'),

	            ],

	        ];

	    }



Hi,

Basically this means:

When you create a new record both timestamps created_at and updated_at are set at the same timestamp? (default behavior)

But when you update a record, the updated_at timestamp does not change?

Did I understand that correct?

If yes:

  • Have you any validation rules inside your model for the created_at and updated_at timestamps?

  • Are other fields with updated information saved?

Regards

Hello MetaCrawler,

Thanks for your reply.

I think, I got the issue behind this.

I have an application page which is throwing random integer. Only on submit the data is getting saved. That’s why the “created_at” and “updated_at” time are same.

[b]Is there any way, for the situation where I can store the refreshed time of the page. For first time when the page refreshed the page it will hold the time which will saved as "created_at" on submit and on submit it will update the "updated_at" time?

[/b]

Please find the attached diagram for the application timeline structure.

Can you help me on this issue please?

Regards,

Sid

Hello Experts,

Any idea/logic on this issue???

Regards,

Sid

Your timestamp behavior should work as expected. As MetaCrawler has pointed, it will set the current time to ‘created_at’ and ‘updated_at’ when a record is newly created and will set the current time only to ‘updated_at’ when a record is updated.

The problem seems to lie in the way you are creating and/or updating the records. I guess you might be creating new records all the time, not updating the existing records.

Hello Softark,

Thanks for your reply.

That’s correct. Every time creating new record. Its not updating the same record later stage.

In my case, when page is getting refreshed and creating some random number, which i am holding in a hidden input form without saving into DB. Once the submit button is pressed, the value which was on hold in the hidden input is getting saved and instantly page get refreshed and throw new set of number.

But the time when the page is created and submission of button, there is time gap, may it be in seconds. But I want to capture that time difference between the page creation time and the model’s save time.

Any idea? Can you help me on this?

Regards,

Sid

Create a new field in your model - something like ‘page_created_at’, and store the time at which the page is created using it.

Hello Softark,

Thanks for the input. I have already created a field in DB.

Currently i am holding the time of the page creation in a text input field, which is getting saved on form submission. Its working.

But I am actually finding some solution, if it can be done through timestamp behviour. If yes, how to do that. Because created_at and updated_at only save time when the model is saved. But the page creation time is few seconds/minutes before the model save.

Any guidance is highly appreciable.

Regards,

Sid

The timestamp behavior works when the model is saved (created or updated). You can’t use it for your purpose.

Hello Softark,

Thanks for the clarification.

We can close this thread now.

Regards,

Sid.