MySQL numeric functions
MySQL string functions
MySQL Date/Time functions
MySQL aggregate functions
MySQL flow control functions
The CURTIME()
and CURRENT_TIME()
functions in MySQL are used to return the current time. Both functions do not require any arguments and they both return the current time in 'HH:MM:SS' format.
Prerequisites:
Tutorial:
To start the mysql
command-line client, open your terminal or command prompt, and enter:
mysql -u [username] -p
Replace [username]
with your MySQL username and enter your password when prompted.
The basic syntax for using the CURTIME()
function is as follows:
SELECT CURTIME();
This will return the current time.
Similarly, you can use the CURRENT_TIME()
function to get the current time:
SELECT CURRENT_TIME();
This will also return the current time.
Both CURTIME()
and CURRENT_TIME()
functions will give you the same result.
Suppose you have a table named events
with a column named event_time
. You can use the CURTIME()
or CURRENT_TIME()
function to find all events happening at the current time like this:
SELECT * FROM events WHERE event_time = CURTIME();
Or
SELECT * FROM events WHERE event_time = CURRENT_TIME();
Both these queries will return a list of events happening at the current time.
EXIT;
By using the CURTIME()
or CURRENT_TIME()
function in MySQL, you can easily get the current time. These functions can be useful for finding records associated with the current time, among other things.
How to Use CURTIME in MySQL:
CURTIME
function is used to retrieve the current time.SELECT CURTIME(); -- Output: Current time in 'HH:MM:SS' format
How to Use CURRENT_TIME in MySQL:
CURRENT_TIME
is an alternative to CURTIME
for fetching the current time.SELECT CURRENT_TIME();
Getting the Current Time in MySQL with CURTIME:
CURTIME
.SELECT CURTIME() AS current_time;
Getting the Current Time in MySQL with CURRENT_TIME:
CURRENT_TIME
.SELECT CURRENT_TIME() AS current_time;
MySQL CURTIME vs CURRENT_TIME Comparison:
SELECT CURTIME() AS curtime_example, CURRENT_TIME() AS current_time_example;
Using CURTIME in SELECT Queries in MySQL:
CURTIME
in your SELECT queries.SELECT user_id, username, last_login_time FROM users WHERE last_login_time > CURTIME() - INTERVAL 1 HOUR;
Using CURRENT_TIME in WHERE Clause in MySQL:
CURRENT_TIME
for time-based WHERE conditions.SELECT appointment_id, appointment_time FROM appointments WHERE appointment_time <= CURRENT_TIME();
Formatting the Current Time in MySQL:
DATE_FORMAT
.SELECT DATE_FORMAT(CURTIME(), '%h:%i %p') AS formatted_time;
MySQL CURTIME and Time Zone Considerations:
CURTIME
.SET time_zone = '+00:00'; SELECT CURTIME();
Performing Time Calculations with CURTIME in MySQL:
CURTIME
.SELECT CURTIME() + INTERVAL 30 MINUTE AS future_time;
Using CURTIME in MySQL Time Comparisons:
CURTIME
.SELECT user_id, username FROM users WHERE last_activity_time < CURTIME() - INTERVAL 1 HOUR;