MySQL | FROM_UNIXTIME method
Start your free 7-days trial now!
MySQL's FROM_UNIXTIME(~)
method returns a date and time from the input unix timestamp.
Parameters
1. unix_timestamp
| timestamp
The unix timestamp to return date and time from.
2. format
| string
| optional
The format that should be applied to the returned date. Defaults to '%Y-%m-%d %T'
.
Refer to 'List of formatting types' at the bottom of page.
Return value
Case | Return Value | Example |
---|---|---|
NOT used in numeric context |
|
|
Used in numeric context |
|
|
The return date and time is expressed in the session time zone.
Examples
String context
To return date and time from unix timestamp 1568452979
:
SELECT FROM_UNIXTIME(1568452979);
+---------------------------+| FROM_UNIXTIME(1568452979) |+---------------------------+| 2019-09-14 18:22:59 |+---------------------------+
Note that the above yields the same result as specifying:
SELECT FROM_UNIXTIME(1568452979, '%Y-%m-%d %T');
+------------------------------------------+| FROM_UNIXTIME(1568452979, '%Y-%m-%d %T') |+------------------------------------------+| 2019-09-14 18:22:59 |+------------------------------------------+
Numeric context
To return date and time as a number from unix timestamp 1568452979
:
SELECT FROM_UNIXTIME(1568452979) + 0;
+-------------------------------+| FROM_UNIXTIME(1568452979) + 0 |+-------------------------------+| 20190914182259 |+-------------------------------+
Format parameter
To return date and time as a long date:
SELECT FROM_UNIXTIME(1568452979,'%W %D %M %Y');
+-----------------------------------------+| FROM_UNIXTIME(1568452979,'%W %D %M %Y') |+-----------------------------------------+| Saturday 14th September 2019 |+-----------------------------------------+
List of formatting types
Specifier | Description |
---|---|
| Abbreviated weekday name (Sun..Sat) |
| Abbreviated month name (Jan..Dec) |
| Month, numeric (0..12) |
| Day of the month with English suffix (0th, 1st, 2nd, 3rd, …) |
| Day of the month, numeric (00..31) |
| Day of the month, numeric (0..31) |
| Microseconds (000000..999999) |
| Hour (00..23) |
| Hour (01..12) |
| Hour (01..12) |
| Minutes, numeric (00..59) |
| Day of year (001..366) |
| Hour (0..23) |
| Hour (1..12) |
| Month name (January..December) |
| Month, numeric (00..12) |
| AM or PM |
| Time, 12-hour (hh:mm:ss followed by AM or PM) |
| Seconds (00..59) |
| Seconds (00..59) |
| Time, 24-hour (hh:mm:ss) |
| Week (00..53), where Sunday is the first day of the week; WEEK() mode 0 |
| Week (00..53), where Monday is the first day of the week; WEEK() mode 1 |
| Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X |
| Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x |
| Weekday name (Sunday..Saturday) |
| Day of the week (0=Sunday..6=Saturday) |
| Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V |
| Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v |
| Year, numeric, four digits |
| Year, numeric (two digits) |
| A literal % character |