Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting epoch number to human readable date in mysql

Tags:

mysql

epoch

I have a epoch number say 1389422614485. The datatype for the value storing this value is varchar. I want to convert its value to human readable time. How can we do it? Any example for this conversion?

like image 201
user3675548 Avatar asked Jun 02 '14 12:06

user3675548


People also ask

How do you convert epoch to human readable date?

Convert from epoch to human-readable dateString date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000)); Epoch in seconds, remove '*1000' for milliseconds. myString := DateTimeToStr(UnixToDateTime(Epoch)); Where Epoch is a signed integer. Replace 1526357743 with epoch.

How do I convert epoch time to manual date?

You can take an epoch time divided by 86400 (seconds in a day) floored and add 719163 (the days up to the year 1970) to pass to it. Awesome, this is as manual as it gets.

How do you read an epoch date?

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).

How does MySQL store epoch time?

You want to use the TIMESTAMP data type. It's stored as an epoch value, but MySQL displays the value as 'YYYY-MM-DD HH:MM:SS'. Show activity on this post. MySql DateTime data type store the date in format 'YYYY-MM-DD HH:MM:SS' with range from '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.


1 Answers

Your epoch value 1389422614485 seems like having the millisecond precision. So you need to use some mysql mathematical functions along with from_unixtime() for generating human readable format.

mysql> select from_unixtime(floor(1389422614485/1000)); +------------------------------------------+ | from_unixtime(floor(1389422614485/1000)) | +------------------------------------------+ | 2014-01-11 12:13:34                      | +------------------------------------------+ 

Update July 2020: As of MySQL 8.0, the floor function is no longer necessary when working with milliseconds:

mysql> select from_unixtime(1594838230234/1000); +------------------------------------------+ | from_unixtime(1594838230234/1000)        | +------------------------------------------+ | 2020-07-15 18:37:10.2340                 | +------------------------------------------+ 
like image 168
Abhik Chakraborty Avatar answered Nov 09 '22 12:11

Abhik Chakraborty