MySQL timestamp is a data type used to represent date and time, usually stored in integer form. Timestamps are widely used in databases, which can record the creation time, modification time and other information of data, and can implement time-related operations and queries. In MySQL, there are two common forms of timestamps, namely UNIX timestamps and DATETIME type timestamps.
In MySQL, you can use the FROM_UNIXTIME() function to convert UNIX timestamps to date and time format. The example is as follows:
SELECT FROM_UNIXTIME(1631365176);
This example converts UNIX timestamp 1631365176 to date time format, and the output result is 2021-09-11 12:06:16.
In addition, you can use the UNIX_TIMESTAMP() function to convert the date and time format into a UNIX timestamp. The example is as follows:
SELECT UNIX_TIMESTAMP('2021-09-11 12:06:16');
This example converts the date and time format '2021-09-11 12:06:16' to a UNIX timestamp, and the output result is 1631365176.
In MySQL, you can directly use the DATETIME type to store timestamps. The example is as follows:
CREATE TABLE example_timestamp ( id INT PRIMARY KEY, created_at DATETIME ); INSERT INTO example_timestamp (id, created_at) VALUES (1, '2021-09-11 12:06:16');
Through the above example, a table named example_timestamp is created, containing the id and created_at fields. , respectively representing the unique identification and creation time of the data.
During data operation, you can use the NOW() function to obtain the current date and time and insert it into a DATETIME type timestamp. The example is as follows:
INSERT INTO example_timestamp (id, created_at) VALUES ( 2, NOW());
This example inserts the current date and time into the table, realizing the function of recording the data creation time.
To sum up, MySQL timestamp is a data type used to represent date and time information and has important applications in databases. Through in-depth discussion and analysis of UNIX timestamps and DATETIME type timestamps, we can better understand the concept and usage of timestamps, so that we can handle time-related logic more flexibly in actual development.
The above is the detailed content of What is a MySQL timestamp? In-depth discussion and analysis. For more information, please follow other related articles on the PHP Chinese website!