MySQL - UNIX_TIMESTAMP() Function



The MySQL UNIX_TIMESTAMP() function converts the date, datetime, or timestamp expression to a UNIX timestamp and returns the result in the form of a string.

This function accepts an optional date expression as an argument and returns the timestamp counted since '1970-01-01 00:00:00' UTC. The return value would be a decimal if the argument given to the function contains a fractional part. If no argument is given, the return value would be an integer, as number of seconds passed from '1970-01-01 00:00:00' UTC to the current timestamp.

Syntax

Following is the syntax of MySQL UNIX_TIMESTAMP() function −

UNIX_TIMESTAMP([expr]);

Parameters

This method accepts either a date-time or a timestamp expression from which you need to get the UNIX timestamp as a parameter.

Return value

This function returns the Unix timestamp value, which is the number of seconds since the Unix epoch (1970-01-01 00:00:00 UTC) corresponding to the specified datetime or the current date.

Example

The following query returns the unix timestamp in seconds as an unsigned integer since '1970-01-01 00:00:00' UTC −

SELECT UNIX_TIMESTAMP() As Result;

Output

Following is the output −

Result
1700557647

Example

If we specify a date or datetime value before '1970-01-01 00:00:00' UTC to this function, it returns 0 as output −

SELECT UNIX_TIMESTAMP('1969-01-01') As Result;

Output

This will produce the following result −

Result
0

Example

The below query will return the unix timestamp in seconds as an unsigned integer from '1970-01-01 00:00:00' UTC for the specified datetime below −

SELECT UNIX_TIMESTAMP('1970-01-01 12:00:00') As Result;

Output

Following is the output −

Result
23400

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

Now, we use the MySQL UNIX_TIMESTAMP() function to calculate the Unix timestamp for all the values in the "DATE" column of ORDERS table −

SELECT OID, DATE, UNIX_TIMESTAMP(DATE) As Result FROM ORDERS;

Output

The output is displayed as follows −

OID DATE Result
102 2009-10-08 00:00:00 1254940200.000000
100 2009-10-08 00:00:00 1254940200.000000
101 2009-11-20 00:00:00 1258655400.000000
103 2008-05-20 00:00:00 1211221800.000000
Advertisements