Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing timestamps in MySQL

Tags:

mysql

The timestamp in my database is in the following format:

2011-02-26T13:00:00-05:00

But getting the current timestamp via MySQL with:

SELECT current_timestamp();

Gives me something formatted like...

2011-02-26 13:05:00

My end goal is to go through all entries (each 2 days) and delete those older than 2 days so how would I compare the entries with the 2011-02-26T13:00:00-05:00 timestamp to the current timestamp?

like image 777
Lothar Avatar asked Feb 27 '11 00:02

Lothar


People also ask

How can I find the difference between two timestamps in MySQL?

To calculate the difference between the timestamps in MySQL, use the TIMESTAMPDIFF(unit, start, end) function. The unit argument can be MICROSECOND , SECOND , MINUTE , HOUR , DAY , WEEK , MONTH , QUARTER , or YEAR . To get the difference in seconds as we have done here, choose SECOND .

Can you compare timestamps in SQL?

Can you compare timestamps in SQL? A date, time, or timestamp value can be compared with another value of the same data type, a datetime constant of the same data type, or with a string representation of a value of that data type.

How do you compare a timestamp?

When comparing two TIMESTAMP WITH TIME ZONE values, the comparison is made using the UTC representations of the values. Two TIMESTAMP WITH TIME ZONE values are considered equal if they represent the same instance in UTC, regardless of the time zone offsets that are stored in the values. For example, '1999-04-15-08.00.

How can I compare two date fields in MySQL?

To count the difference between dates in MySQL, use the DATEDIFF(enddate, startdate) function. The difference between startdate and enddate is expressed in days.


1 Answers

Try:

SELECT DATE(`yourtimestamp`) FROM thetablename WHERE DATE(`yourtimestam`) < CURDATE() - INTERVAL 2 DAY;
like image 200
rg88 Avatar answered Sep 25 '22 00:09

rg88