Sunday, December 22, 2024

MySQL CURRENT_TIME() Function

 The CURRENT_TIME() function in MySQL returns the current time in the format HH:MM:SS based on the server's system time. This function is often used to retrieve the time without the date part. It doesn't take any arguments and is equivalent to CURTIME().

Syntax:

CURRENT_TIME()
  • Returns: The current time (in the format HH:MM:SS), where HH is the hour (00 to 23), MM is the minute (00 to 59), and SS is the second (00 to 59).
  • Timezone: The time returned is based on the system's time zone setting.

Example:

SELECT CURRENT_TIME();

Output:

'12:45:30'

This would return the current time in HH:MM:SS format.

Using CURRENT_TIME() with other queries

  1. Inserting current time into a table: You can use CURRENT_TIME() to insert the current time into a table (only the time part):

    INSERT INTO events (event_time) VALUES (CURRENT_TIME());
    
  2. Comparing with other time values: You can also compare CURRENT_TIME() with other time values. For example, to select records where the event time is greater than the current time:

    SELECT * FROM events WHERE event_time > CURRENT_TIME();
    

Variations:

  • CURRENT_TIME() is equivalent to CURTIME().
  • You can also use CURRENT_TIMESTAMP() if you want both the date and the time.

No comments:

Post a Comment