按日期过滤MYSQL结果

时间:2011-10-29 19:10:56

标签: php mysql

我们有一个每月生成一次发票的脚本(cron)。但我们想添加功能,我们可以选择日期范围“从 - 到”,然后仅为所选日期生成发票。

我想用日历弹出窗口制作输入字段并不难,但使用PHP过滤是一个更大的挑战,所以如果有人想看看我的代码并给我一些提示,我将不胜感激。

    function    genInvoice($clientID, $orderID=0, $paid=false)
{
    if($orderID == 0)
        $sql = "select tblorders.* from tblorders,tblusers where invoiceid=0 and tblorders.userid=tblusers.id " .
            "and status='closed' and tblusers.clientid=" . $clientID;
    else
        $sql = "select tblorders.* from tblorders,tblusers where invoiceid=0 and tblorders.userid=tblusers.id " .
            "and tblusers.clientid=" . $clientID . " and tblorders.id=" . $orderID;
    $res = full_query($sql) or die(mysql_error());
    // If no closed orders uninvoiced, just return
    if(!mysql_num_rows($res))
        return 0;

    $amount = 0;
    $orders = array();
    while($row = mysql_fetch_array($res, MYSQL_ASSOC))
    {
//      print_r($row);
//      print "<br><hr>";
        $amount += $row['amount'];
        $orders[] = $row['id'];
    }
    $date = date("Y-m-d");
    $status = $paid ?'Paid' : 'Unpaid';
    $sql = "insert into tblinvoices (clientid, date, duedate, subtotal, total, status) values (" . $clientID . ",'" . $date .
        "','" . $date . "'," . $amount  . "," . $amount . ",'" .  $status . "')";
    $res = full_query($sql) or die(mysql_error());
    $invoiceid = mysql_insert_id();
    $sql = "update tblorders set invoiceid=" . $invoiceid . " where id in (" . implode(",", $orders) . ")";
    $res = full_query($sql) or die(mysql_error());
    $sql = "select tblorders.id as ReportID, FirstName, LastName, SearchName, CountyID, StateID, bl_orderitems.userid, bl_orderitems.amount, " .
        "bl_orderitems.notes from tblorders, bl_orderitems left join bl_search on bl_search.id=packageid where tblorders.id in (" .
        implode(",", $orders) . ") and bl_orderitems.orderid=tblorders.id order by tblorders.id,bl_orderitems.id";
    $res = full_query($sql) or die(mysql_error());
    while($row = mysql_fetch_array($res, MYSQL_ASSOC))
    {
        if($row['CountyID'] != 0)
            $locale = getCounty($row['CountyID']);
        else if($row['StateID'] != 0)
            $locale = getState($row['StateID']);
        if($row['SearchName'] != "")
            $description = mysql_real_escape_string($row['FirstName'] . " " . $row['LastName'] . " " .
                $row['SearchName'] . " " . $locale . " (Order #" . $row['ReportID'] . ")");
        else
            $description = "Search Package: " . mysql_real_escape_string($row['notes'] . " (Order #" . $row['ReportID'] . ")");
        $sql = "insert into tblinvoiceitems (invoiceid, userid, type, description, amount, duedate) values " .
            "(" . $invoiceid . "," . $row['userid'] . ",'search','" . $description . "','" .
            $row['amount'] . "','" . $date . "')";
//       print $sql . "<br>";
        full_query($sql) or die(mysql_error());
    }
    sendmessage ('Invoice Created', $invoiceid);
    return $invoiceid;
}

1 个答案:

答案 0 :(得分:4)

不会查看所有代码,但按日期范围过滤结果很容易。

SELECT id FROM some_table WHERE some_date_field BETWEEN $first_date AND $second_date