Convert eg. 2012-05-25 to seconds since January 1 1970

netigger picture netigger · Oct 16, 2012 · Viewed 15.6k times · Source

I have a database where I save time as time() from php, which is seconds since 1 jan 1970.

Is there any way I can convert, for example 2012-12-12 to seconds since 1 jan 1970?

I want to do like:

SELECT * 
FROM Table 
WHERE date > '2012-11-30' AND date < '2012-12-30'

Is this even possible?

(I would like to do it without any php date())

Answer

Tim Lehner picture Tim Lehner · Oct 16, 2012

DATEDIFF is going to be your friend here:

select datediff(s, '1970-01-01', '2012-12-12') as SecondsSinceEpoch

Note that because datediff returns an int, the maximum datetime you can compare (using seconds) with Jan 1st, 1970 is 2038-01-19 03:14:07.