Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert timestamp to datetime in MySQL?

Tags:

mysql

How to convert 1300464000 to 2011-03-18 16:00:00 in MySQL?

like image 480
compile-fan Avatar asked Mar 19 '11 15:03

compile-fan


People also ask

How do I convert TIMESTAMP to date?

The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime() method of the TimeStamp class(present in SQL package).

What is difference between TIMESTAMP and datetime in MySQL?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.


Video Answer


2 Answers

Use the FROM_UNIXTIME() function in MySQL

Remember that if you are using a framework that stores it in milliseconds (for example Java's timestamp) you have to divide by 1000 to obtain the right Unix time in seconds.

like image 50
Richard Tuin Avatar answered Sep 28 '22 04:09

Richard Tuin


DATE_FORMAT(FROM_UNIXTIME(`orderdate`), '%Y-%m-%d %H:%i:%s') as "Date" FROM `orders`

This is the ultimate solution if the given date is in encoded format like 1300464000

like image 33
Kingshuk Deb Avatar answered Sep 28 '22 03:09

Kingshuk Deb