Why does FROM_UNIXTIME is returning date back in 1970?

Jaylen

I am trying to get the interval between 2 date.

If I have '2014-10-31' and '2014-10-10' then results needs to be '2014-10-21'

Here is what I have done.

SELECT FROM_UNIXTIME(unix_timestamp(CURDATE()) - unix_timestamp('2014-10-10'), '%Y-%m-%d')

But this is returning 1970-01-22

What I want it to return is 2014-10-21

Why is it not working? and how to get the answer that I am looking for?

Thank you in advance

Eugen Rieck

The result of UNIX_TIMESTAMP() is - you might have guessed - a UNIX timestamp: An integer starting with 0 at 1970-01-01 00:00:00 UTC and counting one for every second since. The important part is, that zero on this timescale is neither 0 A.D. nor some other historic event, but Jan 1st, 1970.

Your example, unix_timestamp(CURDATE()) - unix_timestamp('2014-10-10') calculates a difference of 21 days (as of your posting time) - this translates to a date of 1970-01-01 + 21 days, resulting in 1970-01-22.

EDIT

As from @Mike's question in the comments: Subtracting two dates gives a timespan, not a date. To interpret this as a date again, you need to add it to some starting point in time. As seen above, the UNIX timestamp uses 1970-01-01, while the gregorian calendar uses a ficitve year of Christ's birth. To use this, you would take

DATE_ADD('0001-01-01', INTERVAL (UNIX_TIMESTAMP(CURDATE()) - UNIX_TIMESTAMP('2014-10-10')) SECOND)

resulting in the correct value of 0001-01-22.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Moment.js amDateFormat always returning date from 1970

From Dev

Why is my SQLite query returning the system date as 1970-01-01?

From Dev

Why is my SQLite query returning the system date as 1970-01-01?

From Dev

greenDAO - Date type, returning 1/jan/1970

From Dev

dateWithTimeIntervalSince1970 not returning correct date

From Dev

Parsing date always returning 01 Jan 1970

From Dev

dateWithTimeIntervalSince1970 not returning correct date

From Dev

Why my date is not of current day but shows that of 1970?

From Dev

Returning data from 3m back, or next available date

From Dev

Why does Date.UTC gives back a string of milliseconds

From Dev

Returning by value from a function - Why does it work?

From Dev

date_create_from_format does not give back expected result

From Dev

Returning information back from a JPanel

From Dev

why does date_parse always returning january 1st

From Dev

Why does blocked IO in another thread prevent CreateWindowEx() from returning

From Dev

Returning a date from a class

From Dev

Why does returning this result in an object?

From Dev

Why does it keep returning "null"?

From Dev

Represent date from long, starting from year 0 instead of 1970

From Dev

Android Java: Why does adding weeks worth of miliseconds to a date, makes it go back in time?

From Dev

Why does Android have setTargetFragment() for communicating data back from DialogFragments?

From Dev

Why would a Date constructor with 0 long value as argument will output a date before 1970 in Java?

From Dev

Using strtotime() PHP and revert back trough gmdate() is not returning same date

From Dev

Why is Time.strptime() returning the current date?

From Dev

Why is Time.strptime() returning the current date?

From Dev

If date > curDate returning wrong result, WHY?

From Dev

Hive from_unixtime for milliseconds

From Dev

Hive FROM_UNIXTIME() with milliseconds

From Dev

Returning back to main activity from gameover screen

Related Related

  1. 1

    Moment.js amDateFormat always returning date from 1970

  2. 2

    Why is my SQLite query returning the system date as 1970-01-01?

  3. 3

    Why is my SQLite query returning the system date as 1970-01-01?

  4. 4

    greenDAO - Date type, returning 1/jan/1970

  5. 5

    dateWithTimeIntervalSince1970 not returning correct date

  6. 6

    Parsing date always returning 01 Jan 1970

  7. 7

    dateWithTimeIntervalSince1970 not returning correct date

  8. 8

    Why my date is not of current day but shows that of 1970?

  9. 9

    Returning data from 3m back, or next available date

  10. 10

    Why does Date.UTC gives back a string of milliseconds

  11. 11

    Returning by value from a function - Why does it work?

  12. 12

    date_create_from_format does not give back expected result

  13. 13

    Returning information back from a JPanel

  14. 14

    why does date_parse always returning january 1st

  15. 15

    Why does blocked IO in another thread prevent CreateWindowEx() from returning

  16. 16

    Returning a date from a class

  17. 17

    Why does returning this result in an object?

  18. 18

    Why does it keep returning "null"?

  19. 19

    Represent date from long, starting from year 0 instead of 1970

  20. 20

    Android Java: Why does adding weeks worth of miliseconds to a date, makes it go back in time?

  21. 21

    Why does Android have setTargetFragment() for communicating data back from DialogFragments?

  22. 22

    Why would a Date constructor with 0 long value as argument will output a date before 1970 in Java?

  23. 23

    Using strtotime() PHP and revert back trough gmdate() is not returning same date

  24. 24

    Why is Time.strptime() returning the current date?

  25. 25

    Why is Time.strptime() returning the current date?

  26. 26

    If date > curDate returning wrong result, WHY?

  27. 27

    Hive from_unixtime for milliseconds

  28. 28

    Hive FROM_UNIXTIME() with milliseconds

  29. 29

    Returning back to main activity from gameover screen

HotTag

Archive