compile-fan
compile-fan

Reputation: 17665

How to convert timestamp to datetime in MySQL?

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

Upvotes: 168

Views: 332977

Answers (5)

Dilraj Singh
Dilraj Singh

Reputation: 1011

You can use

select from_unixtime(1300464000,"%Y-%m-%d %h %i %s") from table;

Overall, there are two pertinent methods

  1. from_unixtime()
  2. unix_timestamp()

Upvotes: 14

Kingshuk Deb
Kingshuk Deb

Reputation: 1720

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

Upvotes: 82

Leo
Leo

Reputation: 409

SELECT from_unixtime( UNIX_TIMESTAMP(fild_with_timestamp) ) from "your_table"
This work for me

Upvotes: 4

Richard Tuin
Richard Tuin

Reputation: 4562

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.

Upvotes: 241

ksvendsen
ksvendsen

Reputation: 494

To answer Janus Troelsen comment

Use UNIX_TIMESTAMP instead of TIMESTAMP

SELECT from_unixtime( UNIX_TIMESTAMP(  "2011-12-01 22:01:23.048" ) )

The TIMESTAMP function returns a Date or a DateTime and not a timestamp, while UNIX_TIMESTAMP returns a unix timestamp

Upvotes: 17

Related Questions