MySQL - CURTIME() Function



The TIME, DATETIME and TIMESTAMP datatypes in MySQL are used to store the time, date and time, timestamp values respectively. The time data is usually calculated by counting the number of milliseconds from '1970-01-01 00:00:01' UTC (epoch) to the specified time. MySQL provides a set of functions to manipulate these time values.

MySQL CURTIME() Function

The MySQL CURTIME() function is used to retrieve the current time. The resultant value obtained is a string or a numerical value based on the context and, the time returned will be in the 'hh:mm:ss' or hhmmss format.

Syntax

Following is the syntax of the above function −

CURTIME();

Parameters

This method does not accept any parameters.

Return value

This function returns the current time in the format 'HH:MM:SS', where HH represents hours (00-23), MM represents minutes (00-59), and SS represents seconds (00-59).

Example

Following example demonstrates the usage of the CURTIME() function −

SELECT CURTIME();

Output

Following output is obtained −

CURTIME()
17:50:10

Example

Following is an example of this function in numerical context −

SELECT CURTIME()+0;

Output

Following output is obtained −

CURTIME()+0
175033

Example

You can add seconds to the current time as shown below −

SELECT CURTIME()+12;

Output

Following output is obtained −

CURTIME()+12
175124

Example

We can also subtract the desired number of seconds from the current time using this function −

SELECT CURTIME()-22213;

Output

Following output is obtained −

CURTIME()-22213
152933

Example

Let us create another table with name SALES in MySQL database using CREATE statement as follows −

CREATE TABLE SALES(
   ID INT,
   ProductName VARCHAR(255),
   CustomerName VARCHAR(255),
   DispatchDate date,
   DispatchTime time,
   Price INT,
   Location VARCHAR(255)
);

Now, we will insert 5 records in SALES table using INSERT statements −

INSERT INTO SALES VALUES 
(1, 'Keyboard', 'Rob', DATE('2019-09-01'), TIME('11:00:00'), 7000, 'Hyderabad'),
(2, 'Earphones', 'John', DATE('2019-05-01'), TIME('11:00:00'), 2000, 'Vizag'),
(3, 'Mouse', 'Jane', DATE('2019-03-01'), TIME('10:59:59'), 3000, 'Vijayawada'),
(4, 'Mobile', 'Sam', DATE('2019-03-01'), TIME('10:10:52'), 9000, 'Chennai'),
(5, 'Headset', 'Sara', DATE('2019-04-06'), TIME('11:08:59'), 6000, 'Goa');

Following is another example of this function −

SELECT ProductName, CustomerName, DispatchDate, Price, 
TIMEDIFF(CURTIME(), DispatchTime) as Time_Difference 
FROM SALES;

Output

The output is displayed as follows −

ProductName CustomerName DispatchDate Price Time_Difference
Keyboard Rob 2019-09-01 7000 06:52:52
Earphones John 2019-05-01 2000 06:52:52
Mouse Jane 2019-03-01 3000 06:52:53
Mobile Sam 2019-03-01 9000 07:42:00
Headset Sara 2019-04-06 6000 06:43:53
Advertisements