FROM_UNIXTIME() Examples – MySQL

The MySQL FROM_UNIXTIME() function enables you to return a date representation of a Unix timestamp.

More specifically, it returns the Unix timestamp as a value in ‘YYYY-MM-DD HH:MM:SS’ or YYYYMMDDHHMMSS format, depending on whether the function is used in a string or numeric context.

Syntax

You can use either of the following two forms:

FROM_UNIXTIME(unix_timestamp)
FROM_UNIXTIME(unix_timestamp,format)

The unix_timestamp argument is an internal timestamp value (for example, this could be produced with the UNIX_TIMESTAMP() function).

The (optional) format argument allows you to specify how the result should be formatted. See the table at the bottom of this article for valid formats.

Example 1 – Basic Usage

Here’s an example to demonstrate the first syntax form.

SELECT FROM_UNIXTIME(946562400) AS Result;

Result:

+---------------------+
| Result              |
+---------------------+
| 1999-12-31 00:00:00 |
+---------------------+

Example 2 – Fractional Seconds

In this example, I provide an argument that includes fractional seconds.

SELECT FROM_UNIXTIME(946609199.999999) AS Result;

Result:

+----------------------------+
| Result                     |
+----------------------------+
| 1999-12-31 12:59:59.999999 |
+----------------------------+

Example 3 – Formatting the Result

In this example, I provide a second argument, which specifies how the result is to be formatted.

SELECT FROM_UNIXTIME(946562400, '%W, %D %M %Y') AS Result;

Result:

+----------------------------+
| Result                     |
+----------------------------+
| Friday, 31st December 1999 |
+----------------------------+

And here’s one that includes the time part in the formatting:

SELECT FROM_UNIXTIME(946609199, '%h:%i %p, %D %M %Y') AS Result;

Result:

+------------------------------+
| Result                       |
+------------------------------+
| 12:59 PM, 31st December 1999 |
+------------------------------+

Example 4 – Numeric Context

If FROM_UNIXTIME() is used in a numeric context, the resulting date is provided in numeric format.

Here’s an example that compares results when used in a string context vs numeric.

SELECT 
    FROM_UNIXTIME(946562400) As 'String',
    FROM_UNIXTIME(946562400) + 0 As 'Numeric';

Result:

+---------------------+----------------+
| String              | Numeric        |
+---------------------+----------------+
| 1999-12-31 00:00:00 | 19991231000000 |
+---------------------+----------------+

Available Specifiers

The following specifiers can be used to specify the return format. The format value must start with a percentage sign (%).

Specifier Description
%a Abbreviated weekday name (Sun..Sat)
%b Abbreviated month name (Jan..Dec)
%c Month, numeric (0..12)
%D Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d Day of the month, numeric (00..31)
%e Day of the month, numeric (0..31)
%f Microseconds (000000..999999)
%H Hour (00..23)
%h Hour (01..12)
%I Hour (01..12)
%i Minutes, numeric (00..59)
%j Day of year (001..366)
%k Hour (0..23)
%l Hour (1..12)
%M Month name (January..December)
%m Month, numeric (00..12)
%p AM or PM
%r Time, 12-hour (hh:mm:ss followed by AM or PM)
%S Seconds (00..59)
%s Seconds (00..59)
%T Time, 24-hour (hh:mm:ss)
%U Week (00..53), where Sunday is the first day of the week; WEEK() mode 0
%u Week (00..53), where Monday is the first day of the week; WEEK() mode 1
%V Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%v Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%W Weekday name (Sunday..Saturday)
%w Day of the week (0=Sunday..6=Saturday)
%X Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y Year, numeric, four digits
%y Year, numeric (two digits)
%% A literal % character
%x x, for any “x” not listed above