Select rows from MySQL table where PHP timestamp is older than X

I have a user table in which users must be approved, I want to show users who are not approved and registered more than 7 days ago.

My user_regdate is a timestamp created using the php time () function.

This is what I'm trying to do, it does not work:

mysql_query("select * from users WHERE user_regdate < now() - interval 7 day AND approved='0' order by id;");

thank

+5
source share
4 answers

PHP timstamps is a prime integer, while MySQL now()returns a datetime value. Most likely, this will fix the request:

SELECT ... WHERE user_regdate < unix_timestamp(now() - interval 7 day)) ...

Basically, without calling unix_timstamp (), you are comparing apples and oranges.

+14
source

, MySQL

$timestamp = strtotime("-7 days");
mysql_query("SELECT * FROM users WHERE user_regdate < $timestamp AND approved = 0 ORDER BY id");
+3

php time() unix ( 1 1970 ). MySQL now() (, 2011-6-9 12:45:34)... , .

unix 7 now() :

 $7_days_ago = time() - (7 * 24 * 60 * 60);
 mysql_query("select * from users WHERE user_regdate <" . $7_days_ago . " AND approved='0' order by id;");
+1

;

select * 
from users 
WHERE DATE_SUB(user_regdate,INTERVAL 7 DAY) 
AND approved='0' 
order by id;
0

All Articles