Page 1 of 1

Showing rows posted less than X days ago

Posted: Mon Jul 29, 2013 4:24 am
by Eddy Parera
I'm showing some row from a table and I want only to show rows from 3 days ago or less.

But no luck so far

Code: Select all

<?php
$i=1;
$query = "SELECT id FROM announceaments ORDER BY id DESC LIMIT 0,1";
$result = mysql_query($query) or die(mysql_error());
$grab = mysql_fetch_assoc($result);
$id = $grab['id'];
echo "<div class=\"BoxesWithOuterShadows\">";
if($id==0)
echo "There is no announceaments to show.";
else
while($i<=5)
{
$announceinfo="SELECT * FROM announceaments WHERE id='$id'";
$announceinfo2=mysql_query($announceinfo) or die("could not get account stats!");
$announceinfo3=mysql_fetch_array($announceinfo2);
$title=$announceinfo3['announcetitle'];
$text=$announceinfo3['announceament'];
$time=$announceinfo3['entrydate'];
$date=$announceinfo3['date'];
$title=nl2br($title);
$text=nl2br($text);
$time=nl2br($time);
$3days=date("Y-m-d");
if( strtotime($database_date) > strtotime($3days) &&  strtotime($database_date) < strtotime('now') )
$test='yes';
else
$test='no';
if(isset($title)&&$id>0&&$test=='yes')
{

Re: Showing rows posted less than X days ago

Posted: Mon Jul 29, 2013 9:23 am
by Jackolantern
Is this an application that already has live data? If not, and you are in the design phase, add a timestamp to the table you want to pull based on date, and that will easily allow you to query only for the time frames you want :)

Date-Time manual page
Querying timestamp ranges SO question (look at Dan Grossman's answer, which shows querying for ranges in the MySQL timestamp)

EDIT: Oh, and if you need to know how to make a MySQL timestamp-friendly date for 3 days ago, here it is:

Code: Select all

echo date('Y-m-d', time() - 259200);
For the current time, just remove the - 259200 (the number of seconds in 3 days) part. With this function though, you can make any dates you would need for using the query Dan Grossman gave in that Stack Overflow question (ignore most of the other answers, because some of them are assuming a Unix timestamp, not a MySQL timestamp.

Re: Showing rows posted less than X days ago

Posted: Tue Jul 30, 2013 1:40 pm
by hallsofvallhalla
just out of curiosity is this right in your query?

Code: Select all

announceaments

Re: Showing rows posted less than X days ago

Posted: Thu Aug 08, 2013 12:29 pm
by MikuzA
Hello, the variable you are comparing is not defined.

Code: Select all

$date=$announceinfo3['date'];

Code: Select all

if( strtotime($database_date) > strtotime($3days) &&  strtotime($database_date) < strtotime('now') )
Could this be the issue?