In MySQL, you can use the UTC_TIMESTAMP
function to return the UTC date and time. UTC stands for Coordinated Universal Time and it’s the primary time standard by which the world regulates clocks and time.
The result of this function is returned either in ‘YYYY-MM-DD HH:MM:SS’ or YYYYMMDDHHMMSS format, depending on whether it’s used in a string or numeric context.
Syntax
You can use either of the following two forms:
UTC_TIMESTAMP UTC_TIMESTAMP([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_TIMESTAMP;
Result:
+---------------------+ | UTC_TIMESTAMP | +---------------------+ | 2018-07-06 04:49:11 | +---------------------+
Example 2 – With Parentheses
In this example I add the parentheses (this makes no difference to the result).
SELECT UTC_TIMESTAMP();
Result:
+---------------------+ | UTC_TIMESTAMP() | +---------------------+ | 2018-07-06 04:49:30 | +---------------------+
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_TIMESTAMP(6);
Result:
+----------------------------+ | UTC_TIMESTAMP(6) | +----------------------------+ | 2018-07-06 04:49:49.754432 | +----------------------------+
And in the next example I use 3
to reduce the fractional seconds precision:
SELECT UTC_TIMESTAMP(3);
Result:
+-------------------------+ | UTC_TIMESTAMP(3) | +-------------------------+ | 2018-07-06 04:50:05.489 | +-------------------------+
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_TIMESTAMP() + 0;
Result:
+---------------------+ | UTC_TIMESTAMP() + 0 | +---------------------+ | 20180706045026 | +---------------------+
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_TIMESTAMP() + 0, UTC_TIMESTAMP() + 5;
Result:
+---------------------+---------------------+ | UTC_TIMESTAMP() + 0 | UTC_TIMESTAMP() + 5 | +---------------------+---------------------+ | 20180706045044 | 20180706045049 | +---------------------+---------------------+
Also see UTC_DATE
Examples for returning the UTC date, and UTC_TIME
Examples for returning the UTC time.