I've a table in HTML whichs looks like this:
[Day Before Yesterday] [Yesterday] [Today]
Now I want to spit out the amount of transactions that day, but how day I do that with a query? I've saved the transactions date in unix timestamp, so I was thinking about doing a query like this for today:
$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);
And I think that works fine for today results, but how do I do it for yesterday, that it won't select the the results from today etc? Please help me, because my ideas aren't working..
Use BETWEEN
to get transactions from a particular day.
$two_days_ago = $today - 86400*2;
$query = "SELECT * FROM purchases WHERE date BETWEEN $two_days_ago AND $yesterday";
You can also do the calculations in 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))";