How to use the MySQL CURTIME() function
The CURTIME()
function in MySQL returns the current time in ‘HH:MM:SS’ format. It is useful for inserting and comparing against the current time.
The CURTIME()
function in MySQL returns the current time in ‘HH:MM:SS’ format. It is useful for inserting and comparing against the current time.
Syntax
The syntax for CURTIME()
is simple:
CURTIME()
It takes no arguments.
Examples
-
Get the current time:
SELECT CURTIME();
This returns the current time when the query is executed.
-
Insert current time into a timestamp:
INSERT INTO logs (entry_time) VALUES (CURTIME());
This inserts the current time into the entry_time column.
-
Select records based on current time:
SELECT * FROM events WHERE start_time > CURTIME();
This returns upcoming events with start time greater than current time.
-
Compare against a fixed time:
SELECT CURTIME() > '12:30:00';
This compares the current time against 12:30 PM.
-
Get current date and time together:
SELECT CONCAT(CURDATE(), ' ', CURTIME());
This concatenates current date and time.
Other Similar Functions
CURRENT_TIME()
- Alias forCURTIME()
NOW()
- Current date and timeSYSDATE()
- Current date and timeUNIX_TIMESTAMP()
- Unix timestamp
So CURTIME()
provides an easy way to access the current time for queries and inserts in MySQL.