UTC_TIME Examples – MySQL

In MySQL, you can use the UTC_TIME function to return the UTC time. UTC stands for Coordinated Universal Time and it’s the primary time standard by which the world regulates clocks and time.

The result is returned either in ‘HH:MM:SS’ or HHMMSS format, depending on whether the function is used in a string or numeric context.

Syntax

You can use either of the following two forms:

UTC_TIME
UTC_TIME([fsp])

Where fsp is an optional argument that specifies the fractional seconds precision to use in the result.

Example 1 – Basic Usage

Here’s an example to demonstrate.

SELECT UTC_TIME;

Result:

+----------+
| UTC_TIME |
+----------+
| 22:38:38 |
+----------+

Example 2 – With Parentheses

In this example I add the parentheses (of course, this makes no difference to the outcome).

SELECT UTC_TIME();

Result:

+------------+
| UTC_TIME() |
+------------+
| 22:38:52   |
+------------+

Example 3 – Fractional Seconds Precision

Here I add an argument that specifies the fractional seconds precision to use. In this case I use 6, which means that the result will have a precision down to the millisecond.

SELECT UTC_TIME(6);

Result:

+-----------------+
| UTC_TIME(6)     |
+-----------------+
| 22:45:19.912591 |
+-----------------+

And in the next example I use 3 to reduce the fractional seconds precision:

SELECT UTC_TIME(3);

Result:

+--------------+
| UTC_TIME(3)  |
+--------------+
| 22:45:55.415 |
+--------------+

Example 4 – Numerical Context

The previous examples were all returned in ‘HH:MM:SS’ format. This is because they were used in a string context.

In this example I use the function in a numerical context. I do this by adding a number to the function.

SELECT UTC_TIME() + 0;

Result:

+----------------+
| UTC_TIME() + 0 |
+----------------+
|         223914 |
+----------------+

In this case I added zero, so the result is now in HHMMSS format.

There’s nothing to stop you from adding another number. Example:

SELECT 
  UTC_TIME() + 0,
  UTC_TIME() + 5;

Result:

+----------------+----------------+
| UTC_TIME() + 0 | UTC_TIME() + 5 |
+----------------+----------------+
|         224051 |         224056 |
+----------------+----------------+

Also see UTC_DATE Examples for returning the UTC date.