Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a user table, where users need to be approved, i want to show users who are not approved and is registered more than 7 days ago.

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

This is what i try, it does not works:

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

Thanks

like image 935
2by Avatar asked Jun 09 '11 20:06

2by


2 Answers

PHP's timstamps are a simple integer, whereas MySQL's now() returns a datetime value. Most likely this will fix up the query:

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

Basically, without the unix_timstamp() call, you're comparing apples and oranges.

like image 199
Marc B Avatar answered Nov 03 '22 16:11

Marc B


Primitive solution at best, but im not the best at MySQL time calculation

$timestamp = strtotime("-7 days");
mysql_query("SELECT * FROM users WHERE user_regdate < $timestamp AND approved = 0 ORDER BY id");
like image 3
Mick Hansen Avatar answered Nov 03 '22 15:11

Mick Hansen