Then, in the Editor Type, set as "one line edit", and set the predefined editor value as the placeholder %CURRENT_DATETIME% :
And then, if someone tries to add a new row, this column's value will automatically be default as the current date and time :
-
2.
There is also a custom SQL workaround, for SQL tables, if you need a 'timestamp' :
1. Create a column (for example "Updated") in your table
2. In column settings set the type to be "DateTime" (in the "Data" tab), and in the "Editing" tab set the "Editor Input Type" to "none".
3. In the database, run this query:
ALTER TABLE `table_name` CHANGE `updated` `updated` DATETIME NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Please note that you need to replace 'table_name' with the actual database table name. You can find the database name of the table in the Editing tab above the table, in the "MySQL query name for editing" field.
When adding a new record with a datetime field how can I make it default to the current date and time?
Hi , Raymond
Thanks for reaching out to us
For example, If you need a simple auto pre-populate for the column Date and Time, to be the current Date Time,
we have this placeholder,
that you can use as a predefined editor value.
-
Here is how you can do it :
Set column type to DateTime;
Then, in the Editor Type, set as "one line edit", and set the predefined editor value as the placeholder %CURRENT_DATETIME% :
And then, if someone tries to add a new row, this column's value will automatically be default as the current date and time :
-
2.
There is also a custom SQL workaround, for SQL tables, if you need a 'timestamp' :
1. Create a column (for example "Updated") in your table
2. In column settings set the type to be "DateTime" (in the "Data" tab), and in the "Editing" tab set the "Editor Input Type" to "none".
3. In the database, run this query:
Please note that you need to replace 'table_name' with the actual database table name. You can find the database name of the table in the Editing tab above the table, in the "MySQL query name for editing" field.
-
Let us know if this was helpful.
Kind Regards,
Miloš Jovanović
[email protected]
Rate my support
Try our FREE mapping plugin! MapSVG - easy Google maps, interactive SVG maps and floor plans, choropleth maps and much more - https://wordpress.org/plugins/mapsvg-lite-interactive-vector-maps/
wpDataTables: FAQ | Facebook | Twitter | Instagram | Front-end and back-end demo | Docs
Amelia: FAQ | Facebook | Twitter | Instagram | Amelia demo sites | Docs | Discord Community
You can try wpDataTables add-ons before purchasing on these sandbox sites:
Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables