MySQL - FROM_UNIXTIME() Function



The MySQL FROM_UNIXTIME() function is used to return a representation of UNIX timestamp as a datetime or character string value. UNIX timestamp is nothing but an internal timestamp value representing number of seconds since '1970-01-01 00:00:00' UTC.

This function accepts a UNIX timestamp as argument and the format as an optional argument, converts it into datetime value or a character string (according to the format, if given) and returns the result.

Special cases of this function includes −

  • If format is omitted, this function returns a DATETIME value.

  • If UNIX timestamp or format is NULL, this function returns NULL.

  • If UNIX timestamp is an integer, the fractional seconds precision of the DATETIME is zero.

  • When UNIX timestamp is a decimal value, the fractional seconds precision of the DATETIME is the same as the precision of the decimal value, up to a maximum of 6.

  • When UNIX timestamp is a floating point number, the fractional seconds precision of the datetime is 6.

Syntax

Following is the syntax of MySQL FROM_UNIXTIME() function −

FROM_UNIXTIME(unix_timestamp, [format])

Parameters

This method accepts two parameters. The same is described below −

  • unix_timestamp: The UNIX timestamp, representing the number of seconds since January 1, 1970, 00:00:00 UTC.

  • format (optional): The desired format for the output. It's a string specifying how the result should be formatted. If omitted, the default format is 'YYYY-MM-DD HH:MM:SS'.

Return value

This function returns a DATETIME value representing the date and time corresponding to the given UNIX timestamp.

Example

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

SELECT FROM_UNIXTIME(0) As Result;

Output

Following output is obtained −

Result
1970-01-01 05:30:00

Example

Following is another example of this function, where we are converting the Unix timestamp 1351708200 into a date and time format −

SELECT FROM_UNIXTIME(1351708200) As Result;

Output

Following output is obtained −

Result
2012-11-01 00:00:00

Example

We can also pass the date-time expression as an argument to this function −

SELECT FROM_UNIXTIME(1441426245.2300) As Result;

Output

This will produce the following result −

Result
2015-09-05 09:40:45.2300

Example

We can also pass timestamp values in the form of a string as shown below −

SELECT FROM_UNIXTIME('463455563') As Result;

Output

Following output is obtained −

Result
1984-09-08 07:09:23.000000

Example

We can also pass the column name as an argument to this function. Let us create a table named MyPlayers using CREATE statement as shown below −

CREATE TABLE MyPlayers(
   ID INT,
   NAME VARCHAR(255),
   DOBTimestamp VARCHAR(255),
   Place_Of_Birth VARCHAR(255),
   Country VARCHAR(255),
   PRIMARY KEY (ID)
);

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

INSERT INTO MyPlayers VALUES
(1, 'Shikhar Dhawan', '376338600', 'Delhi', 'India'),
(2, 'Jonathan Trott', '356725800', 'CapeTown', 'SouthAfrica'),
(3, 'Kumara Sangakkara', '246738600', 'Matale', 'Srilanka'),
(4, 'Virat Kohli', '594671400', 'Delhi', 'India'),
(5, 'Rohit Sharma', '546719400', 'Nagpur', 'India'),
(6, 'Ravindra Jadeja', '597349800', 'Nagpur', 'India'),
(7, 'James Anderson', '394223400', 'Burnley', 'England');

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

Select * From MyPlayers;

Following is the ORDERS table −

IDNAMEDOBTimestampPlace_Of_BirthCountry
1Shikhar Dhawan376338600DelhiIndia
2Jonathan Trott356725800CapeTownSouthAfrica
3Kumara Sangakkara246738600MataleSrilanka
4Virat Kohli594671400DelhiIndia
5Rohit Sharma546719400NagpurIndia
6Ravindra Jadeja597349800NagpurIndia
7James Anderson394223400BurnleyEngland

Now, we use the MySQL FROM_UNIXTIME() function to convert the values of a DOBTimestamp column into datetime values −

SELECT ID, NAME, DOBTimestamp, FROM_UNIXTIME(DOBTimestamp) 
As Result From MyPlayers;

Output

The output is displayed as follows −

IDNAMEDOBTimestampResult
1Shikhar Dhawan3763386001981-12-05 00:00:00.000000
2Jonathan Trott3567258001981-04-22 00:00:00.000000
3Kumara Sangakkara2467386001977-10-27 00:00:00.000000
4Virat Kohli5946714001988-11-05 00:00:00.000000
5Rohit Sharma5467194001987-04-30 00:00:00.000000
6Ravindra Jadeja5973498001988-12-06 00:00:00.000000
7James Anderson3942234001982-06-30 00:00:00.000000