How do you insert a timestamp in MySQL?

How to Easily Add Timestamps to Your MySQL Tables

Inserting a timestamp is fundamental to database management, allowing developers and analysts to accurately track when records were created or modified. In MySQL, this process is flexible and powerful. You can effortlessly insert the current date and time using the built-in NOW() function, ensuring automated record keeping. Alternatively, when dealing with legacy data or external sources, you might need to supply a specific date and time string. In such cases, the STR_TO_DATE() function is indispensable for converting various date formats into the required internal format before insertion. Regardless of the method chosen, the timestamp is inserted into a designated column using the standard INSERT INTO statement, establishing a reliable audit trail for all data operations.


Understanding the TIMESTAMP Data Type in MySQL

The TIMESTAMP data type is specifically designed for storing temporal values, making it an essential component for tracking when events occur within a database environment. When defining your table structure in MySQL, using TIMESTAMP for creation or modification columns is highly recommended for auditing and data integrity purposes. Unlike the DATETIME type, TIMESTAMP values are stored efficiently and automatically converted between the current time zone and UTC (Coordinated Universal Time) upon retrieval, simplifying timezone management. This automatic conversion makes TIMESTAMP a popular choice for globally distributed applications.

When preparing data for insertion into a TIMESTAMP column, the value must conform strictly to a predefined structure. This standardization ensures that the database engine can correctly parse and store the moment in time. Adherence to this format is critical to avoid syntax or data conversion errors during the insertion process.

Standard Format Requirements for MySQL Timestamps

For direct insertion without using conversion functions, the TIMESTAMP value must be supplied as a string literal that follows the exact structure shown below. This format is internationally recognized and facilitates smooth interaction with the database system.

When inserting timestamps, they must be in the following standard format:

'YYYY-MM-DD HH:MM:SS'

This format is composed of the following mandatory components:

  • YYYY: The year, expressed using four numerical digits (e.g., 2024).
  • MM: The month, expressed using two numerical digits (01 through 12).
  • DD: The day of the month, expressed using two numerical digits (01 through 31).
  • HH: The hour, based on a 24-hour clock, expressed using two numerical digits (00 through 23).
  • MM: The minutes, expressed using two numerical digits (00 through 59).
  • SS: The seconds, expressed using two numerical digits (00 through 59).

It is important to note that modern TIMESTAMP implementations also allow for the inclusion of fractional seconds, which can extend up to six digits of precision (microseconds). This allows for highly detailed temporal tracking, especially useful in high-frequency logging or transaction systems.

For example, the string literal ‘2022-10-28 04:15:56.003495‘ is a perfectly valid timestamp, capturing the moment down to the microsecond level.

Important Considerations: Range Limits and Storage

While the TIMESTAMP data type is highly versatile, it is essential for database architects to be aware of its range limitations. The TIMESTAMP column has a defined range that extends from ‘1970-01-01 00:00:01’ UTC up to ‘2038-01-19 03:14:07’ UTC. This limitation is tied to how the value is stored internally (as a 32-bit integer representing seconds since the Epoch). If your application requires storing dates outside of this range (e.g., historical records prior to 1970 or future dates past 2038), the DATETIME data type should be used instead, as it offers a much wider temporal range, albeit without the automatic timezone conversion feature.

The following example demonstrates how to practically implement a TIMESTAMP column within a table structure in MySQL, setting the stage for tracking specific events.

Practical Example: Defining and Inserting Timestamp Data

To illustrate the implementation of timestamps, we will construct a table named sales. This table is intended to store crucial information about sales transactions, including the precise moment each sale occurred. We use the TIMESTAMP data type for the sales_time column, ensuring that every record accurately captures its time of creation.

We use the following SQL syntax to create the table structure and then populate it with five initial rows of sample data:

-- create table 
CREATE TABLE sales (
  store_ID INT PRIMARY KEY,
  item TEXT NOT NULL,
  sales_time TIMESTAMP NOT NULL
);

-- insert rows into table
INSERT INTO sales VALUES (0001, 'Oranges', '2015-01-12 03:45:00');
INSERT INTO sales VALUES (0002, 'Apples', '2020-11-25 15:25:01');
INSERT INTO sales VALUES (0003, 'Bananas', '2009-06-30 09:01:39');
INSERT INTO sales VALUES (0004, 'Melons', '2022-04-09 03:29:55');
INSERT INTO sales VALUES (0005, 'Grapes', '2023-05-19 23:10:04');

-- view all rows in table
SELECT * FROM sales;

The resulting output confirms that the data has been inserted successfully, with the sales_time column containing values precisely formatted as required by the TIMESTAMP data type.

Output:

+----------+---------+---------------------+
| store_ID | item    | sales_time          |
+----------+---------+---------------------+
|        1 | Oranges | 2015-01-12 03:45:00 |
|        2 | Apples  | 2020-11-25 15:25:01 |
|        3 | Bananas | 2009-06-30 09:01:39 |
|        4 | Melons  | 2022-04-09 03:29:55 |
|        5 | Grapes  | 2023-05-19 23:10:04 |
+----------+---------+---------------------+

Observe that the sales_time column, defined as a TIMESTAMP column, displays each of the inserted timestamps formatted as YYYY-MM-DD HH:MM:SS. This successful insertion hinges entirely on providing the timestamp string in the canonical MySQL format.

Handling Format Errors and Data Type Conversions

A common challenge when dealing with date and time data, especially when integrating data from various sources, is format incompatibility. If you attempt to insert a timestamp string that deviates from the expected YYYY-MM-DD HH:MM:SS structure, the MySQL engine will enforce strict data integrity rules, leading to an error. This mechanism prevents invalid data from corrupting your records.

Consider the following attempt, where the date is provided in the common U.S. format (MM/DD/YYYY) rather than the required standard format:

-- insert row into table
INSERT INTO sales VALUES (0006, 'Cabbage', '7/22/2023 05:56:00');

-- view all rows in table
SELECT * FROM sales;

The immediate consequence of using the incorrect format is a runtime error:

ERROR 1292 (22007): Incorrect datetime value: '7/22/2023 05:56:00' for column 'sales_time' at row 1

This error confirms that the string ‘7/22/2023 05:56:00’ cannot be implicitly converted into a valid TIMESTAMP value by the database.

Utilizing STR_TO_DATE() for Flexible Input

When faced with data provided in non-standard temporal formats, the solution lies in explicit data conversion. STR_TO_DATE() is a powerful MySQL function that allows you to specify the exact format of the input string, instructing the database how to parse it correctly before storing the resulting timestamp.

To successfully insert the previous failing record, we use STR_TO_DATE(), providing the input string (‘7/22/2023 05:56:00’) and the corresponding format mask (‘%m/%d/%Y %H:%i:%s’). This tells MySQL that the month comes first (%m), followed by the day (%d), and so on.

-- insert row into table
INSERT INTO sales VALUES (0006, 'Cabbage', STR_TO_DATE('7/22/2023 05:56:00', '%m/%d/%Y %H:%i:%s'));

-- view all rows in table
SELECT * FROM sales;

The result now includes the new record, demonstrating successful conversion and insertion:

Output:

+----------+---------+---------------------+
| store_ID | item    | sales_time          |
+----------+---------+---------------------+
|        1 | Oranges | 2015-01-12 03:45:00 |
|        2 | Apples  | 2020-11-25 15:25:01 |
|        3 | Bananas | 2009-06-30 09:01:39 |
|        4 | Melons  | 2022-04-09 03:29:55 |
|        5 | Grapes  | 2023-05-19 23:10:04 |
|        6 | Cabbage | 2023-07-22 05:56:00 |
+----------+---------+---------------------+

By employing STR_TO_DATE, we effectively transform the non-standard date string into a valid, canonical TIMESTAMP format, allowing the insertion to proceed without generating any errors. This technique is invaluable for robust data handling in production environments.

Conclusion

Mastering timestamp insertion in MySQL is essential for maintaining accurate temporal data. Whether you rely on automatic current time insertion or complex string conversion using functions like STR_TO_DATE(), adhering to the required format ensures data integrity and operational consistency. Proper use of the TIMESTAMP data type allows for reliable auditing and analysis across your application’s lifecycle.

The following tutorials explain how to perform other common tasks in MySQL:


MySQL: How to Insert Datetime

Cite this article

mohammed looti (2026). How to Easily Add Timestamps to Your MySQL Tables. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-insert-a-timestamp-in-mysql/

mohammed looti. "How to Easily Add Timestamps to Your MySQL Tables." PSYCHOLOGICAL SCALES, 6 Jan. 2026, https://scales.arabpsychology.com/stats/how-do-you-insert-a-timestamp-in-mysql/.

mohammed looti. "How to Easily Add Timestamps to Your MySQL Tables." PSYCHOLOGICAL SCALES, 2026. https://scales.arabpsychology.com/stats/how-do-you-insert-a-timestamp-in-mysql/.

mohammed looti (2026) 'How to Easily Add Timestamps to Your MySQL Tables', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-insert-a-timestamp-in-mysql/.

[1] mohammed looti, "How to Easily Add Timestamps to Your MySQL Tables," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, January, 2026.

mohammed looti. How to Easily Add Timestamps to Your MySQL Tables. PSYCHOLOGICAL SCALES. 2026;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top