MySQL FROM_UNIXTIME() Function

In MySQL, the FROM_UNIXTIME() function will convert unix_timestamp to a datetime and formats it as a string if a format string is specified.

FROM_UNIXTIME() Syntax

Here is the syntax of MySQL FROM_UNIXTIME() function:

FROM_UNIXTIME(unix_timestamp[, format])

Parameters

unix_timestamp
Required. UNIX timestamp is the number of seconds since UTC time 1970-01-01 00:00:00.
format
Optional. It is used to format date/time values. For more information about formatting pattern, see DATE_FORMAT().

Return value

The MySQL FROM_UNIXTIME() function will convert unix_timestamp to a datetime and formats it as a string if format is specified.

The FROM_UNIXTIME() function will return a datetime value without format argument; Otherwise, it will return a string in the format format.

If any argument is NULL, the FROM_UNIXTIME() function will return NULL.

FROM_UNIXTIME() Examples

Convert UNIX timestamp to UTC time:

SELECT FROM_UNIXTIME(1649839394);
+---------------------------+
| FROM_UNIXTIME(1649839394) |
+---------------------------+
| 2022-04-13 08:43:14       |
+---------------------------+

Convert a UNIX timestamp to UTC and format the datetime:

SELECT FROM_UNIXTIME(1649839394, '%Y%m%d%H%i%S');
+-------------------------------------------+
| FROM_UNIXTIME(1649839394, '%Y%m%d%H%i%S') |
+-------------------------------------------+
| 20220413084314                            |
+-------------------------------------------+