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.

Posted on

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

  1. Get the current time:

    SELECT CURRENT_TIME();
    

    This returns the current time when the query is executed.

  2. 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.

  3. 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.

  4. Compare CURRENT_TIME() to a fixed time:

    SELECT CURRENT_TIME() = '22:15:30';
    

    This compares the current time against the fixed time.

  5. 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 for CURRENT_TIME()
  • NOW() - Current date and time
  • SYSDATE() - Current date and time
  • CURRENT_TIMESTAMP - Current timestamp

So CURRENT_TIME() provides an easy way to access the current time for queries and inserts in MySQL.