The CURRENT_TIMESTAMP() function in MySQL returns the current date and time in the format YYYY-MM-DD HH:MM:SS or as a numeric value in the YYYYMMDDHHMMSS.uuuuuu format.
Definition and Usage
- The
CURRENT_TIMESTAMP()function returns the current date and time.
Syntax
CURRENT_TIMESTAMP()
Technical Details
- Works in: From MySQL 4.0
- Return Value: Returns the current date and time in the format
YYYY-MM-DD HH:MM:SS.
Examples
Example 1: Return current date and time
SELECT CURRENT_TIMESTAMP();
Output:
2024-12-28 14:30:15
This query will return the current date and time in the YYYY-MM-DD HH:MM:SS format.
Example 2: Return current date and time + 1 second
SELECT CURRENT_TIMESTAMP() + 1;
Output:
2024-12-28 14:30:16
This query adds 1 second to the current timestamp and returns the updated date and time.
Related Functions
NOW(): Equivalent toCURRENT_TIMESTAMP().CURDATE(): Returns the current date.CURTIME(): Returns the current time.CURRENT_DATE(): Returns the current date (equivalent toCURDATE()).CURRENT_TIME(): Returns the current time (equivalent toCURTIME()).
Use Cases
- Logging Events: When you need to store the timestamp of an event,
CURRENT_TIMESTAMP()is ideal. - Time Calculations: It is useful in date-time arithmetic or when comparing timestamps for query filtering.
- Default Values: You can use
CURRENT_TIMESTAMP()to set the default value of a column as the current date and time when inserting records.