I’ve this table in my database:
------------------------------------------------- | id | name | model | inserted_time (timestamp) | -------------------------------------------------
When I insert something into my table the inserted_time get’s set automatically so this is the current timestamp.
Now I need to do a select on my table to get all entries wich are 3 days older then the inserted_time timestamp. I’m doing this in PHP with wpdb in WordPress. This is what I already have:
$results = $wpdb->get_results( 'SELECT * FROM my_table WHERE inserted_time /*OLDER THEN 3 DAYS */' );
For example when I’ve following entry in my table:
------------------------------------------------- | id | name | model | inserted_time (timestamp) | ------------------------------------------------- | 1 | James | xxx | 2018-11-14 21:20:04 | -------------------------------------------------
So when I do my select now and the current timestamp is 2018-11-15 11:20:04 I don’t want my entry in my results object / array (or what ever it is).
But when we have this current timestamp 2018-11-17 21:20:05 I need this in my results variable.
So I’m absolutely stumped with this. All my other stuff around this works fine but this is still a problem.
Is it possible to do this only with SQL? Or do I need to pass a parameter like $current_timestamp?
Advertisement
Answer
You can use now()
or current_timestamp
:
SELECT * FROM my_table WHERE inserted_time < now() - interval 3 day;