What are the methods to convert timestamps in MySQL?
In MySQL, you can convert a timestamp to a date and time using the following methods:
- By using the function FROM_UNIXTIME(), you can convert a UNIX timestamp into a standard date and time format. For example:
- Convert the Unix timestamp 1615545895 into a date and time.
- By using the DATE_FORMAT() function, you can format a date or time value into a specified string format. When converting a timestamp to a date and time, you can first convert the timestamp to date format and then use the DATE_FORMAT() function to format the date into the specified string format. For example:
- Convert the Unix timestamp 1615545895 to a formatted date and time in the format ‘YYYY-MM-DD HH:mm:ss’.
- By using the CONVERT() function, you can transform an expression into a specified data type. When converting a timestamp into a date and time, you can use the CONVERT() function to convert the timestamp into a DATETIME data type. For example:
- SELECT DATETIME FROM_UNIXTIME(1615545895);
These methods allow users to select the appropriate method to convert timestamps into dates and times as needed.