每天从日期中选择mysql数据

时间:2017-05-11 08:50:24

标签: php mysql sql

我在HTML中的表格看起来像这样:

[昨天前一天] [昨天] [今天]

现在我想吐出那天的交易数量,但是我用查询做了多少天呢?我已经将交易日期保存在unix时间戳中,所以我正考虑今天进行这样的查询:

$today = time();

   $yesterday = $today - 86400;

   //Query for getting today amount of purchases

   $query = "SELECT * FROM purchases WHERE date > $yesterday";

   $result = mysqli_query($con,$query);

   $todayamount = mysqli_num_rows($result);

而且我觉得这对于今天的结果很好,但我如何在昨天做到这一点,它不会选择今天的结果等?请帮助我,因为我的想法不起作用..

1 个答案:

答案 0 :(得分:1)

使用BETWEEN获取特定日期的交易。

$two_days_ago = $today - 86400*2;
$query = "SELECT * FROM purchases WHERE date BETWEEN $two_days_ago AND $yesterday";

您也可以在MySQL中进行计算:

$query = "SELECT * FROM purchases 
          WHERE date BETWEEN UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 DAY)) AND UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))";