Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL select formatted date from millisecond field

Tags:

date

sql

time

mysql

I have a column in a MySQL database that contains a date as milliseconds (epoch). I want to build an SQL query that formats the date as something human readable (day, month, year, hours, minutes, seconds in any format and time zone). Is there an SQL (or MySQL-specific) function to do this?

like image 953
Jason C Avatar asked Aug 11 '13 19:08

Jason C


People also ask

How do I pick a date from a TIMESTAMP?

In MySQL, use the DATE() function to retrieve the date from a datetime or timestamp value. This function takes only one argument – either an expression which returns a date/datetime/ timestamp value or the name of a timestamp/datetime column.

How do I select a specific date in MySQL?

You can use DATE() from MySQL to select records with a particular date. The syntax is as follows. SELECT *from yourTableName WHERE DATE(yourDateColumnName)='anyDate'; To understand the above syntax, let us first create a table.

How can get date in dd mm yyyy format in MySQL?

MySQL DATE_FORMAT() Function The DATE_FORMAT() function formats a date as specified.

How are dates formatted in MySQL?

MySQL retrieves and displays DATE values in ' YYYY-MM-DD ' format. The supported range is '1000-01-01' to '9999-12-31' . The DATETIME type is used for values that contain both date and time parts.


2 Answers

Try using the FROM_UNIXTIME function like this as given in the manual

 SELECT FROM_UNIXTIME(1196440219);  -> '2007-11-30 10:30:19' 

You could also use formatting like this

 mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),     ->                      '%Y %D %M %h:%i:%s %x');     -> '2007 30th November 10:30:59 2007' 
like image 120
woofmeow Avatar answered Sep 29 '22 19:09

woofmeow


If you want to get the microsecond from a field, use %f,

select FROM_UNIXTIME(DATE_COLUMN/1000,'%Y-%M-%d %H:%i:%s %f') from TABLE_NAME;  +-------------------------------------------------------+ | FROM_UNIXTIME(CREATETIME/1000,'%Y-%M-%d %H:%i:%s %f') | +-------------------------------------------------------+ | 2016-March-18 16:02:54 342000                         | +-------------------------------------------------------+ 

Source : MYSQL DATE_FORMAT

like image 40
AbD_Riz Avatar answered Sep 29 '22 20:09

AbD_Riz