MySQL - UTC_TIME() Function



The MySQL UTC_TIME() function returns the current Coordinated Universal Time (UTC) as a time value. The resultant value 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.

This function does not accept any arguments and just returns the current time. But we can still pass an optional argument to specify the number of digits to be displayed in the fractional part of seconds.

Syntax

Following is the syntax of MySQL UTC_TIME() function −

UTC_TIME();

Parameters

This method does not accept any parameters.

Return value

This function returns the current UTC (Coordinated Universal Time) time as a TIME value in the format 'HH:MM:SS'.

Example

In the following example, we are using the MySQL UTC_TIME() function to fetch the current UTC time −

SELECT UTC_TIME() As Result;

Output

This will produce the following result −

Result
10:03:47

Example

We can also use UTC_TIME instead of UTC_TIME() to retrieve the current UTC time −

SELECT UTC_TIME As Result;

Output

Following is the output −

Result
10:03:47

Example

Here, we are adding seconds to the UTC current time as shown below in the below query −

SELECT UTC_TIME()+12 As Result;

Output

Output of the above code is as follows −

Result
100359

Example

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

SELECT UTC_TIME()-300 As Result;

Output

We get the output as shown below −

Result
100047

Example

This function accepts an optional argument i.e. fsp, using this we can specify the number of digits we need after the fraction for seconds −

SELECT UTC_TIME(5) As Result;

Output

The result obtained is as shown below −

Result
10:03:47.24164

In this example, we have created a table named SALES using the following CREATE TABLE query −

CREATE TABLE SALES(
   NAME varchar(255),
   PRODUCT varchar(255),
   DISPATCH_DATE date,
   DISPATCH_TIME time,
   LOCATION varchar(255)
);

Now, insert the following records into the SALES table using the INSERT statement −

INSERT INTO SALES VALUES
('Aadhya', 'Key-Board', DATE('2019-05-04'), TIME('18:00:00'), 'Kerala'),
('Varun', 'Earphones', DATE('2019-06-26'), TIME('19:00:00'), 'Mumbai'),
('Vratha', 'Mouse', DATE('2019-12-07'), TIME('20:00:00'),'Vijayawada'),
('Rahul', 'Mobile', DATE('2018-03-21'), TIME('21:00:00'), 'Chennai'),
('Maaya', 'Headset', DATE('2018-12-30'), TIME('22:00:00'), 'Goa');

Execute the below query to fetch all the inserted records in the above-created table −

Select * From SALES;

Following is the ORDERS table −

NAME PRODUCT DISPATCH_DATE DISPATCH_TIME LOCATION
Aadhya Key-Board 2019-05-04 18:00:00 Kerala
Varun Earphones 2019-06-26 19:00:00 Mumbai
Vratha Mouse 2019-12-07 20:00:00 Vijayawada
Rahul Mobile 2018-03-21 21:00:00 Chennai
Maaya Headset 2018-12-30 22:00:00 Goa

The below query retrieves the time difference between "DISPATCH_TIME" and "UTC_TIME" columns from the "SALES" table −

SELECT NAME, DISPATCH_TIME, TIMEDIFF(DISPATCH_TIME, UTC_TIME)
As Result FROM SALES;

Output

The output is displayed as follows −

CUST_NAME DISPATCH_TIME Result
Aadhya 18:00:00 07:29:33
Varun 19:00:00 08:29:33
Vratha 20:00:00 09:29:33
Rahul 21:00:00 10:29:33
Maaya 22:00:00 11:29:33
Advertisements