169

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

3

5 Answers 5

243

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.

Sign up to request clarification or add additional context in comments.

4 Comments

SELECT from_unixtime( TIMESTAMP( "2011-12-01", "22:01:23.048" ) ) doesn't work. why? using v5.6
@JanusTroelsen SELECT from_unixtime( unix_timestamp(TIMESTAMP( "2011-12-01", "22:01:23.048" ) ) ) works. TIMESTAMP() does not return an integer value.
Just because I've done this and been confused at the output: MySQL stores Unix time in seconds, whereas a lot of other frameworks store it in milliseconds (i.e. Java's timestamp). So just remember to divide by 1000 if you're using this function on data coming from somewhere that uses millis for Unix time.
Also tested to work in MariaDB. If you want only the date, for example, then add a second parameter like select from_unixtime(1234567890, '%W %D %M %x');
84
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

2 Comments

Thanks for this. To include the time: DATE_FORMAT(FROM_UNIXTIME(`orderdate`), '%d-%m-%Y %H:%i:%s') as "Date" FROM `orders`
I'm pretty sure DATE_FORMAT(FROM_UNIXTIME(orderdate), '%Y-%m-%d') as "Date" FROM orders would be the ultimate solution :D
17

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

3 Comments

seems clear to compare a unix time from a unix time. I use SELECT (CAST(DATE_FORMAT(from_unixtime(UNIX_TIMESTAMP(CURRENT_TIMESTAMP)), '%Y-%m-%d') as DATE) < CAST('2019-05-02' AS DATE)) ; to compare dates, I replace 2019-05*02 a formatted datetime object in php. Thnaks.
Please do not answer this way to the different question than the original one.
This does not answer the original question in any way
14

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()

2 Comments

I would use this format for military time "%Y-%m-%d %H %i %s"
as @tannerburton says it's H not h as the OP wanted 24 hour time format
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.