Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql adding hours to datetime

Tags:

datetime

mysql

In MYSQL DB I need to check if a "datetime" field is more than 24hours (or whatever) ago in which case delete the row.

How to add hours to datetime in mysql?

thanks

Luca

like image 734
luca Avatar asked Mar 24 '11 11:03

luca


2 Answers

What about something like this :

delete
from your_table
where your_field <= date_sub(now(), INTERVAL 1 day)


With :

  • now() : the current date time
  • date_sub() to substract 1 day to that date


Or, if you want o use 24 hours instead of 1 day :

delete
from your_table
where your_field <= date_sub(now(), INTERVAL 24 hour)
like image 56
Pascal MARTIN Avatar answered Nov 14 '22 06:11

Pascal MARTIN


You have the Date and Time functions.

WHERE `yourDate` < DATE_SUB(NOW(),INTERVAL 1 DAY)

or shorter

WHERE `yourDate` < NOW() - INTERVAL 1 DAY
like image 11
Alin Purcaru Avatar answered Nov 14 '22 07:11

Alin Purcaru