MySQL - UTC_DATE() Function



The MySQL UTC_DATE() is used to get the current UTC date. The resultant value is a string or a numerical value based on the context and, the date returned will be in the 'YYYY-MM-DD' or YYYYMMDD format.

UTC is short for Coordinated Universal Time and defined as a time standard that is commonly used across the world. It is written in the 24 hour format and is kept using highly precise atomic clocks in combination with the Earth's rotation. UTC is different from time zones as local time zones are just referred to as the offsets of the UTC.

This function does not accept any arguments and just simply returns the current time.

Syntax

Following is the syntax of MySQL UTC_DATE() function −

UTC_DATE();

Parameters

This method does not accept any parameters.

Return value

This function returns the current date in UTC (Coordinated Universal Time) format. The returned value is a date in 'YYYY-MM-DD' format, representing the year, month, and day.

Example

In the following query, we are using the MySQL UTC_DATE() function to return the current UTC date −

SELECT UTC_DATE() As Result;

Output

This will produce the following result −

Result
0

Example

We can also add days to the current UTC date as shown in the below query −

SELECT UTC_DATE()+10 As Result;

Output

Following is the output −

Result
20231131

Example

We can also subtract the desired number of days from the current UTC date using this function −

SELECT UTC_DATE()-20 As Result;

Output

Following is the output −

Result
20231101

Example

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

CREATE TABLE ORDERS (
   OID INT NOT NULL,
   DATE VARCHAR (20) NOT NULL,
   CUSTOMER_ID INT NOT NULL,
   AMOUNT DECIMAL (18, 2)
);

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

INSERT INTO ORDERS VALUES 
(102, '2009-10-08 00:00:00', 3, 3000.00),
(100, '2009-10-08 00:00:00', 3, 1500.00),
(101, '2009-11-20 00:00:00', 2, 1560.00),
(103, '2008-05-20 00:00:00', 4, 2060.00);

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

Select * From ORDERS;

Following is the ORDERS table −

OID DATE CUSTOMER_ID AMOUNT
102 2009-10-08 00:00:00 3 3000.00
100 2009-10-08 00:00:00 3 1500.00
101 2009-11-20 00:00:00 2 1560.00
103 2008-05-20 00:00:00 4 2060.00

The below query calculates the difference in days between the current UTC date and the date in the "ORDERS" table −

SELECT OID, DATE, DATEDIFF(UTC_DATE(), DATE) 
As Result FROM ORDERS;

Output

The output is displayed as follows −

OID DATE Result
102 2009-10-08 00:00:00 5157
100 2009-10-08 00:00:00 5157
101 2009-11-20 00:00:00 5114
103 2008-05-20 00:00:00 5663
Advertisements