MySQL - LOCALTIMESTAMP() Function



The MYSQL LOCALTIMESTAMP() function is used to get the current date and time value. The resultant value is a string or a numerical value based on the context and, the value returned will be in the 'YYYY-MM-DD hh:mm:ss' or YYYYMMDDhhmmss format.

We can also use LOCALTIMESTAMP instead of LOCALTIMESTAMP(). These are also synonyms for the NOW() function. The MySQL NOW() function is used to get the current date and time value.

Syntax

Following is the syntax of MySQL LOCALTIMESTAMP() function −

LOCALTIMESTAMP();

Parameters

This method does not accept any parameters.

Return value

This function returns the current date and time value in 'YYYY-MM-DD hh:mm:ss' or YYYYMMDDhhmmss format.

Example

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

SELECT LOCALTIMESTAMP() As Result;

Output

This will produce the following result −

Result
2023-11-13 17:31:58

Example

In the below query, we are adding 0 to the current local time. The "+0" serves as a simple way to convert the time to a numeric representation

SELECT LOCALTIMESTAMP()+0 As Result;

Output

Following is the output −

Result
20231113173158

Example

We can add seconds to the current time stamp as shown below −

SELECT LOCALTIMESTAMP()+12 As Result;

Output

Following is the output −

Result
20231113173170

Example

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

SELECT LOCALTIMESTAMP()-12 As Result;

Output

Following is the output −

Result
20231113173146

Example

We can use LOCALTIMESTAMP instead of LOCALTIMESTAMP() to retrieve the current time.

SELECT LOCALTIMESTAMP As Result;

Output

Following is the output −

Result
2023-11-13 17:31:58

Example

In the following query, we have created a table named ORDERS using the CREATE query as shown below −

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 following query calculates the time difference in minutes between the "DATE" column and the current local timestamp using TIMESTAMPDIFF function −

SELECT OID, DATE, LOCALTIME, TIMESTAMPDIFF(MINUTE, DATE, LOCALTIMESTAMP) 
As Result FROM ORDERS;

Output

The output is displayed as follows −

OID DATE LOCALTIME Result
102 2009-10-08 00:00:00 2023-11-13 17:35:27 7415615
100 2009-10-08 00:00:00 2023-11-13 17:35:27 7415615
101 2009-11-20 00:00:00 2023-11-13 17:35:27 7353695
103 2008-05-20 00:00:00 2023-11-13 17:35:27 8144255
Advertisements