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

2019-03-20 07:58发布

问题:

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())

回答1:

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.