How to use the MySQL CURRENT_TIME() function
The CURRENT_TIME()
function in MySQL returns the current time in ‘HH:MM:SS’ format. It can be used to insert or query time values based on the current time.
The CURRENT_TIME()
function in MySQL returns the current time in ‘HH:MM:SS’ format. It can be used to insert or query time values based on the current time.
Syntax
The syntax for CURRENT_TIME()
is simple:
CURRENT_TIME()
It takes no arguments.
Examples
-
Get the current time:
SELECT CURRENT_TIME();
This returns the current time when the query is executed.
-
Insert the current time into a timestamp column:
INSERT INTO logs (entry_time) VALUES (CURRENT_TIME());
This inserts the current time as the entry_time for the log entry.
-
Select records based on current time:
SELECT * FROM events WHERE start_time < CURRENT_TIME();
This returns upcoming events whose start time is later than the current time.
-
Compare CURRENT_TIME() to a fixed time:
SELECT CURRENT_TIME() = '22:15:30';
This compares the current time against the fixed time.
-
Get current date and time together:
SELECT CONCAT(CURDATE(),' ',CURRENT_TIME());
This returns current date and time together in a string.
Other Similar Functions
CURTIME()
- Alias forCURRENT_TIME()
NOW()
- Current date and timeSYSDATE()
- Current date and timeCURRENT_TIMESTAMP
- Current timestamp
So CURRENT_TIME()
provides an easy way to access the current time for queries and inserts in MySQL.