昨天和今天在mysql中的日期

时间:2012-09-04 23:35:16

标签: php mysql time

我的数据库中的日期列采用以下格式: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);

3 个答案:

答案 0 :(得分: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()更改为您选择的日期。

答案 1 :(得分:1)

试试这个:

$yesterday= date("Y-m-d", time()-86400)." 00:00:00";
$yesterday2= date("Y-m-d", time()-86400)." 23:59:59";

答案 2 :(得分: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