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.

Posted on

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:

  1. Convert a Unix timestamp to DATETIME:

    SELECT FROM_UNIXTIME(1196440219);
    
    // Output: 2007-11-30 10:10:19
    
  2. Select dates greater than a given timestamp:

    SELECT * FROM records
    WHERE date > FROM_UNIXTIME(1258532000);
    
  3. Calculate the Unix timestamp of a DATETIME:

    SELECT UNIX_TIMESTAMP('2023-02-15 13:02:35');
    
    // Output: 1676434955
    
  4. Add days to a timestamp:

    SELECT FROM_UNIXTIME(1196440219 + 86400*10);
    
    // 10 days after timestamp
    
  5. 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 value
  • DATE_FORMAT(): Formats datetime values
  • NOW(): Returns current date and time

So in summary, FROM_UNIXTIME() converts Unix timestamps to MySQL date/time values.