Summary: in this tutorial, you will learn how to use the MySQL CURRENT_TIME
function to get the current time.
Introduction to MySQL CURRENT_TIME function
The CURRENT_TIME
function returns the current time. Here’s the syntax of the CURRENT_TIME
function:
CURRENT_TIME
Code language: SQL (Structured Query Language) (sql)
The CURRENT_TIME
doesn’t accept any arguments and returns the current time value in 'hh:mm:ss'
if it is used in a string context and hhmmss
if it is used in a numeric context.
Besides the CURRENT_TIME
function, you can use the CURRENT_TIME()
function or CURTIME()
function to get the current time. And you can use these functions interchangeably because they’re synonyms.
Note that you cannot use
MySQL CURRENT_TIME function example
Let’s take some examples of using the CURRENT_TIME
function.
1) Simple CURRENT_TIME function examples
The following example uses the CURRENT_TIME
function to get the current time:
SELECT CURRENT_TIME;
Code language: SQL (Structured Query Language) (sql)
Output:
+--------------+
| CURRENT_TIME |
+--------------+
| 09:36:20 |
+--------------+
1 row in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)
In this example, the CURRENT_TIME
function returns the current time as a string in the format 'hh:mm:ss'
. The reason is that we use the CURRENT_TIME
function in the string context.
If we use the CURRENT_TIME
function in a numeric context, it’ll return the current time in the hhmmss
format as shown in the following example:
SELECT CURRENT_TIME + 0;
Code language: SQL (Structured Query Language) (sql)
Output:
93620
Code language: SQL (Structured Query Language) (sql)
The output 93620 has three parts:
- Hour: 9
- Minutes: 36
- Seconds: 20
2) Using the MySQL CURRENT_TIME with table data
In the following example, we’ll show you how to use the CURRENT_TIME
function to query data from a table.
First, create a new table called events
with the following structure:
CREATE TABLE events (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
start_date DATE NOT NULL,
start_time TIME NOT NULL
);
Code language: SQL (Structured Query Language) (sql)
Second, insert some rows into the events
table:
INSERT INTO events (name, start_date, start_time)
VALUES
('Tech Conference', '2023-10-18', '10:00:00'),
('AI Summit', '2023-10-18', '14:30:00'),
('WebDev World', '2023-10-18', '09:45:00');
Code language: SQL (Structured Query Language) (sql)
Third, use the CURRENT_TIME
function to get the events that have not yet started on 2023-10-18
:
SELECT
*
FROM
events
WHERE
start_date = '2023-10-18'
and start_time > CURRENT_TIME;
Code language: SQL (Structured Query Language) (sql)
Output:
+----+-----------+------------+------------+
| id | name | start_date | start_time |
+----+-----------+------------+------------+
| 2 | AI Summit | 2023-10-18 | 14:30:00 |
+----+-----------+------------+------------+
1 row in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)
Note that the query will return the result based on your current time when you run it.
Default current time value
Note that you cannot use the CURRENT_TIME
, CURRENT_TIME()
, or CURTIME()
function as a default value for a column in a table. Hence, the following statements will result in an error:
CREATE TABLE test(
started_at TIME DEFAULT CURRENT_TIME
);
CREATE TABLE test(
started_at TIME DEFAULT CURRENT_TIME()
);
CREATE TABLE test(
started_at TIME DEFAULT CURTIME()
);
Code language: SQL (Structured Query Language) (sql)
To use the current time as a default value for a column, you use the DATETIME or TIMESTAMP as the column’s type and the NOW() function as the default value. For example:
CREATE TABLE test(
started_at DATETIME DEFAULT CURTIME()
);
Code language: SQL (Structured Query Language) (sql)
Summary
- Use the
CURRENT_TIME
function to get the current time as a string in the format'hh:mm:ss'
or as a number in the formathhmmss
.