Only show certain items in a mysql database using php and time()
Is there a way to only show the items that are older than a certain date in a php mysql array? I'm using this query method and sorting by lastpost_cl:
$query="SELECT * FROM properties ORDER BY lastpost_cl";
$result=mysql_query($query);
$num = mysql_num_rows ($result);
mysql_close();
and I was thinking the way of checking the time would be:
if (time() <= strtotime($lastpost_cl)) {
}
How can I combine the two and only show lastpost_cl that a开发者_如何转开发re older that the current time?
$query="SELECT * FROM properties WHERE lastpost_cl < NOW() ".
"ORDER BY lastpost_cl";
This assumes you're storing the date using one of mysql date types. See the manual here.
MySQL can do that on its own. Just change your query to this:
$query="SELECT * FROM properties WHERE lastpost_cl < NOW() ORDER BY lastpost_cl";
You should do this in the sql query though, because the time on the sql server and web server could be different. A query like this would get everything older than a week
SELECT
*
FROM properties
where lastpost_cl < now() - INTERVAL 7 DAY
ORDER BY lastpost_cl
精彩评论