1

I have the following table in a MySQL database:

+----------+---------------------+-----------------+---------------+
| id       | firstName           | lastName        | tstamp        |
+----------+---------------------+-----------------+---------------+
| 133      | James               | Mosher          | 1589470887919 |  
| 145      | Bill                | Turner          | 1589470888235 | 
| 146      | Jeremy              | Vine            | 1589470888592 |  
| 152      | Ramon               | Jesus           | 1589470889319 |
+----------+---------------------+-----------------+---------------+

I want to be able to see the tstamp column in a human readable date time format. I tried the following query but it didn't work and gave me a blank column. Anyone has any idea or see what I am doing wrong?

SELECT id, firstName, lastName, tstamp, FROM_UNIXTIME(tstamp, '%Y %D %M %H:%i:%s') AS timeAndDate FROM students;

Result:

+----------+---------------------+-----------------+---------------+---------------+
| id       | firstName           | lastName        | tstamp        | timeAndDate   |
+----------+---------------------+-----------------+---------------+---------------+
| 133      | James               | Mosher          | 1589470887919 |               | 
| 145      | Bill                | Turner          | 1589470888235 |               |
| 146      | Jeremy              | Vine            | 1589470888592 |               |
| 152      | Ramon               | Jesus           | 1589470889319 |               |
+----------+---------------------+-----------------+---------------+---------------+

Example -> https://www.db-fiddle.com/f/nfSnTKfNYdBcbs8pDgbcdQ/0

2
  • 1
    FWIW, I would handle this in application code, formatting the time to suit the locale of the reader Commented Jul 6, 2020 at 12:03
  • @Strawberry - Yes, you are right but I do not own the application code that generates this. Only have MySql access to data for now. Commented Jul 6, 2020 at 12:04

2 Answers 2

1

The timestamps contain milliseconds.
You can remove them if you divide by 1000:

SELECT id, firstName, lastName, tstamp, 
  FROM_UNIXTIME(tstamp / 1000, '%Y %D %M %H:%i:%s') AS timeAndDate 
FROM myTable;

See the demo.

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

Comments

1

See comments, but I guess you're after something like this...

SELECT DATE_FORMAT(FROM_UNIXTIME(1589470887919/1000),'%Y %d %m %H:%i:%s') x;
+---------------------+
| x                   |
+---------------------+
| 2020 14 05 16:41:27 |
+---------------------+

2 Comments

There is no need for DATE_FORMAT(). The function FROM_UNIXTIME() takes the format as the 2nd argument.
@forpas - oh, right - good to know; thanks, that's much more elegant!

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.