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.

Posted on

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

  1. Get the current time:

    SELECT CURTIME();
    

    This returns the current time when the query is executed.

  2. Insert current time into a timestamp:

    INSERT INTO logs (entry_time)
    VALUES (CURTIME());
    

    This inserts the current time into the entry_time column.

  3. Select records based on current time:

    SELECT * FROM events
    WHERE start_time > CURTIME();
    

    This returns upcoming events with start time greater than current time.

  4. Compare against a fixed time:

    SELECT CURTIME() > '12:30:00';
    

    This compares the current time against 12:30 PM.

  5. Get current date and time together:

    SELECT CONCAT(CURDATE(), ' ', CURTIME());
    

    This concatenates current date and time.

Other Similar Functions

  • CURRENT_TIME() - Alias for CURTIME()
  • NOW() - Current date and time
  • SYSDATE() - Current date and time
  • UNIX_TIMESTAMP() - Unix timestamp

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