PHP/SQL -显示在特定日期之后创建的行


PHP/SQL - Display Rows Created After a Certain Date

我创建了类RankingsDateFilter

每个排名类都有一个DateFilter类,它应该产生截止日期。我试图能够创建一个过滤器,以便在该日期之后创建的所有内容都将显示在表中。

然而,这种比较不起作用。你能看出问题所在吗?

这是我的dateffilter类:

<?php
include ("Filter.php");
class DateFilter extends Filter
{
    //@param daysOld: how many days can be passed to be included in filter
    //Ex. If daysOld = 7, everything that is less than a week old is included
    private $interval;
    public function DateFilter($daysOld)
    {
        $this->interval = new DateInterval('P'.$daysOld.'D');
    }
    //@Return: Returns a DateTime that is the earliest possible date to be included in the filter
    function createLimitDate()
    {
        $now = new DateTime();
        return $now->sub($this->interval);
    }
    //generates SQL code for checking date
    //Ex. WHERE limitDate > created... if > means before
    function genSQL()
    {
        $limitDate = $this->createLimitDate();
        return $limitDate->format('Y-m-d') . " < 'created'";
    }
}
?>

And my Rankings Class:

<?php
class Rankings 
{
    private $filter;
    //@params: $filty is the filter given to these rankings
    public function Rankings($filty)
    {
        $this->filter = $filty;
    }
    //@return: returns the html code for the rankings
    public function display()
    {
        echo '<table border="1" align="center">'.
                    '<tr align="center" style="font-weight:bold;">
                        <b><td>#</td><td>NAME</td><td>Date</td></b>
                    </tr>
                    ';
            //hardcoding DB
            $where = $this->filter->genSQL();
            $qry = mysql_query("SELECT * FROM  `pix` 
                                WHERE $where
                                ");
                if (!$qry)
                    die("FAIL: " . mysql_error());
            $i = 1;
            while($row = mysql_fetch_array($qry))
            {
                $name = $row['uniquename'];
                $created = $row['created'];
                echo ' <tr>
                            <td>'. $i . '</td>'.
                            '<td>' . $name . '</td>'.
                            '<td>'. $created . '</td>'.
                        '</tr>';
                $i += 1;
            }
            echo '</table>';
            echo $where;
    }
}
?>

我这样调用它:

$test = new DateFilter(100);
$rankings = new Rankings($test);
$rankings->display();

在这个示例中,没有显示任何内容,尽管我确信数据库中的所有内容都是在不到100天前上传的。

在传递给MySQL的日期周围加上引号,并在列名周围加上引号:

return "'" . $limitDate->format('Y-m-d') . "' < created";