Mysql method to convert time to datetime: use [FROM_UNIXTIME (time value)] in the sql statement, the code is [insert into test(time) values(FROM_UNIXTIME(%d))", time(NULL) 】.
Mysql method to convert time to datetime:
FROM_UNIXTIME(time(NULL)) convert liunx system The time_t type time is converted into the datetime type time of mysql::
Use FROM_UNIXTIME (time value);
in the sql statement to convert the time value, The time_t type is converted to the datetime type of mysql.
create table test ( id int(11) auto_increment primary key, time datetime not null ); char sqlbuf[4096]; snprintf(sqlbuf,sizeof[sqlbuf],"insert into test(time) values(FROM_UNIXTIME(%d))",time(NULL)); //这样使用
UNIX_TIMESTAMP (field name of datatime type) Convert the datetime type of mysql to the time_t type::
Use UNIX_TIMESTAMP in sql data (Field name of datatime type field);
Convert datetime type value to time_t type
char sqlbuf[4096];
snprintf (sqlbuf,sizeof(sqlbuf),"select UNIX_TIMESTAMP(time) valuetime from test"); //You can take out the datetime type value and rename the field to valuetime
++++++++++++++++ create table user( id int unsigned not null primary key auto_increment, username char(40) not NULL, normterm char default NULL, smdtime datetime default NULL); insert into user values(1, 'Jone', NULL, NULL);
More related free learning recommendations: mysql tutorial(video)
The above is the detailed content of How to convert time to datetime in mysql. For more information, please follow other related articles on the PHP Chinese website!