How to set current time for timestamp and datetime type using db_schema file in magento - Magento Source24

Hello magento friends here we are explane that How can the timestamp and datetime types in the Magento object save repository be set to the current time?

Date and date/time values are entered into database tables using MySQL's Timestamp and DateTime types.


➤ Both types have a default parameter called default="CURRENT TIMESTAMP" that will utilise the current time specified in the database as the current time.

You can specify the default attribute with the value CURRENT TIMESTAMP to save the current time while adding an entry to the database table while dealing with the db schema.xml file and need to add or update the current time in the table for any individual object save repository.


➤ In the field for the timestamp type,


How to set current time for timestamp and datetime type using db_schema file  in magento



<table name="my_table_name" resource="default" engine="innodb" comment="My table name">
    <column xsi:type="timestamp" name="created_at" nullable="false" default="CURRENT_TIMESTAMP"
        comment="Created At"/>
</table>


➤ For DateTime type field,


How to set current time for timestamp and datetime type using db_schema file  in magento



<table name="my_table_name" resource="default" engine="innodb" comment="My table name">
    <column xsi:type="datetime" name="created_at" nullable="false" default="CURRENT_TIMESTAMP"
    comment="Created At"/>
</table>


To save the current time in the table, we need to set default="CURRENT TIMESTAMP" in the column created at.

When you want to keep track of the time that each entry is added to the tables, this will be useful.

The table column will automatically be set with the current time.





Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post