How to convert 1300464000 to 2011-03-18 16:00:00 in MySQL?
-
2Please search for existing questions/answers before you post a new question - this topic has already been covered in some detail.John Parker– John Parker2011-03-19 15:04:14 +00:00Commented Mar 19, 2011 at 15:04
-
2Have you had a look at the documentation already? It's all there: dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.htmlFelix Kling– Felix Kling2011-03-19 15:05:35 +00:00Commented Mar 19, 2011 at 15:05
-
2possible duplicate of Convert timestamp to a readable date during queryJohn Parker– John Parker2011-03-19 15:06:09 +00:00Commented Mar 19, 2011 at 15:06
Add a comment
|
5 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.
4 Comments
Janus Troelsen
SELECT from_unixtime( TIMESTAMP( "2011-12-01", "22:01:23.048" ) ) doesn't work. why? using v5.6cnvzmxcvmcx
@JanusTroelsen
SELECT from_unixtime( unix_timestamp(TIMESTAMP( "2011-12-01", "22:01:23.048" ) ) ) works. TIMESTAMP() does not return an integer value.Chris Thompson
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.
Luc
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');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
JNP Web Developer
Thanks for this. To include the time:
DATE_FORMAT(FROM_UNIXTIME(`orderdate`), '%d-%m-%Y %H:%i:%s') as "Date" FROM `orders` Kyle
I'm pretty sure DATE_FORMAT(FROM_UNIXTIME(
orderdate), '%Y-%m-%d') as "Date" FROM orders would be the ultimate solution :DTo 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
Mantisse
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.
Paweł Stankowski
Please do not answer this way to the different question than the original one.
Nico Haase
This does not answer the original question in any way
You can use
select from_unixtime(1300464000,"%Y-%m-%d %h %i %s") from table;
Overall, there are two pertinent methods
- from_unixtime()
- unix_timestamp()
2 Comments
tanner burton
I would use this format for military time "%Y-%m-%d %H %i %s"
Kevin
as @tannerburton says it's
H not h as the OP wanted 24 hour time format