Ivo
Ivo

Reputation: 3

How to select a php date() format in SQL

I have an SQL table with a field "time_created" in the form "Wed, 19 Aug 2015 03:58:00 -0600". I need to check this field against today and perform a where statement according to it, as in

SELECT * FROM table WHERE UNIX_TIMESTAMP() - UNIX_TIMESTAMP(time_created) >= 3600*24*30

to select records where time_created is older then a month.

Problem is UNIX_TIMESTAMP doesn't understand formats as "Wed, 19 Aug 2015 03:58:00 -0600". Any other way?

Upvotes: 0

Views: 29

Answers (1)

Phil Cross
Phil Cross

Reputation: 9302

Try:

SELECT *
FROM table 
WHERE DATE(NOW()) > DATE_SUB(STR_TO_DATE(time_created, '%a, %e %b %Y %T'), INTERVAL 1 MONTH)

This will find all records where the current date (DATE(NOW())) is bigger than time_created subtract ` month.

Upvotes: 1

Related Questions