8

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

Tim Lehner
  • 14,813
  • 4
  • 59
  • 76
netdigger
  • 3,659
  • 3
  • 26
  • 49

1 Answers1

17

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.

Tim Lehner
  • 14,813
  • 4
  • 59
  • 76