How to use the MySQL FROM_UNIXTIME() function
The FROM_UNIXTIME() function in MySQL converts a Unix timestamp to a DATETIME value. It is useful for working with timestamps from other systems or programming languages.
The FROM_UNIXTIME() function in MySQL converts a Unix timestamp to a DATETIME value. It is useful for working with timestamps from other systems or programming languages.
Syntax
The basic syntax of FROM_UNIXTIME() is:
FROM_UNIXTIME(unix_timestamp)
Where unix_timestamp
is an integer Unix timestamp.
Examples
Here are some examples of using FROM_UNIXTIME() in MySQL:
-
Convert a Unix timestamp to DATETIME:
SELECT FROM_UNIXTIME(1196440219); // Output: 2007-11-30 10:10:19
-
Select dates greater than a given timestamp:
SELECT * FROM records WHERE date > FROM_UNIXTIME(1258532000);
-
Calculate the Unix timestamp of a DATETIME:
SELECT UNIX_TIMESTAMP('2023-02-15 13:02:35'); // Output: 1676434955
-
Add days to a timestamp:
SELECT FROM_UNIXTIME(1196440219 + 86400*10); // 10 days after timestamp
-
Convert timestamp to specific date format:
SELECT DATE_FORMAT(FROM_UNIXTIME(1196440219), '%Y-%m-%d'); // Output: 2007-11-30
Other Similar Functions
UNIX_TIMESTAMP()
: Returns a Unix timestamp for a datetime valueDATE_FORMAT()
: Formats datetime valuesNOW()
: Returns current date and time
So in summary, FROM_UNIXTIME() converts Unix timestamps to MySQL date/time values.