Get the App
SLTechnology News&Howtos  ›  Database  › 

The method of converting time to datetime by mysql

Shulou Source: shulou.com Published: 2022-06-01 02:03:40 10月31日 Update

Editor to share with you mysql to achieve time to datetime method, I hope you will learn a lot after reading this article, let's discuss it together!

Mysql implements the method of converting time to datetime: use [FROM_UNIXTIME (time value)] in the sql statement, and the code is [insert into test (time) values (FROM_UNIXTIME (% d)) ", time (NULL)].

The method of converting time to datetime by mysql:

FROM_UNIXTIME (time (NULL)) converts the time_t type time of the liunx system to the datetime type time of mysql:

Use FROM_UNIXTIME (time value) in sql statements

Convert the time value, time_t type 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)); / / used in this way

UNIX_TIMESTAMP (field name of datatime type) converts the datetime type of mysql to time_t type::

Use UNIX_TIMESTAMP (the field name of a field of type datatime) in sql data

Convert the value of type datetime to type time_t

Char sqlbuf [4096]

Snprintf (sqlbuf,sizeof (sqlbuf), "select UNIX_TIMESTAMP (time) valuetime from test"); / / you can take out the value of type datetime 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); after reading this article, I believe you have some understanding of mysql's method of converting time to datetime. Want to know more about it. Welcome to follow the industry information channel. Thank you for reading!

Tags: Type time method field middle use article statement code done data more knowledge system industry information information channel channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei Docker MariaDB MySQL