I’m a newbie in php and WordPress. I’m trying to create a script that will store data into a custom table in wordpress. I want the script to store the current date and time in the mysql database. I have created a column in the Mysql table with it’s data type set to DATETIME but when I try to insert data into the table, the current date is not being shown. Waht might be the problem? My script is shown below.
$wpdb->insert($table,array('account_id' => $account_id, 'billing_first_name'=>$first_name,'billing_last_name' => $last_name, 'feature_level' => 'FreeAccount','created_at'=> date("Y-m-d, h:i a"),'billing_interval' => "Manual"));
I think there is an issue with the date format you are passing in the query.
Considering created_at as the column in MySql having
DATETIMEformat, you should pass created_at in the correct format. I thinkDATETIMEfield accepts date and time in formatYYYY-MM-DD HH:MM:SS.Can you try with this query:
Here the only change in the query is the create_at field value you are passing it to MySql.
If created_at is not the
DATETIMEfield in db, and you have another column that you set asDATETIMEand you want to get it updated when you insert a record, then change the db structure to automatically update theDATETIMEfield on update. Set Default asCURRENT TIMESTAMP. So even if you don’t pass value to that field it will automatically get updated.Hope this is what you want.
🙂