0

我数据库中的日期列采用以下格式:date('Y-M-j H:i:s');. 我正在尝试运行一个查询来完成今天的所有事务,另一个查询昨天完成的所有事务。请帮助最好的方法来做到这一点。

我尝试了以下但它不起作用

 $yesterday = date("Y-M-j 00:00:01", strtotime("-1 day"));
$yesterday2 = date("Y-M-j 23:59:59", strtotime("-1 day"));

$query_syesterday = "SELECT sum(amount), sum(amtnaira) FROM transactions WHERE transtype = 'buy' AND batch !='' AND date BETWEEN '$yesterday' AND '$yesterday2'";
$syesterday = mysql_query($query_syesterday, $egold1) or die(mysql_error());
$row_syesterday = mysql_fetch_assoc($syesterday);
$totalRows_syesterday = mysql_num_rows($syesterday);
4

3 回答 3

2

这应该这样做:

$query_last_two_days = "SELECT sum(amount), sum(amtnaira) 
   FROM transactions 
      WHERE transtype = 'buy' 
        AND batch !='' 
        AND date < DATE_SUB(NOW(), INTERVAL 2 DAY)";

您可以像这样轻松获得最后两天。您可以更改NOW()为您选择的日期。

于 2012-09-05T00:28:43.953 回答
1

您使用了错误的日期格式。改用这个:

$yesterday = date("Y-m-d", strtotime("-1 day"));

此外,只需比较日期,而不是时间:

SELECT SUM(amount), 
       SUM(amtnaira) 
FROM   transactions 
WHERE  transtype = 'buy' 
       AND batch != '' 
       AND DATE(date) = $yesterday
于 2012-09-04T23:40:51.900 回答
1

尝试这个:

$yesterday= date("Y-m-d", time()-86400)." 00:00:00";
$yesterday2= date("Y-m-d", time()-86400)." 23:59:59";
于 2012-09-04T23:39:01.520 回答