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())
Days Since 1970-01-01 There were 19307 days since January 1, 1970, the Unix epoch.
Convert from human-readable date to epoch long epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.
The getTime method returns the number of milliseconds since the Unix Epoch (1st of January, 1970 00:00:00). To get a Unix timestamp, we have to divide the result from calling the getTime() method by 1000 to convert the milliseconds to seconds. What is this?
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With