1

I have a table like this:

// Times
+----+-------------+
| id |  timestamp  |   // echo date('d/m/Y', $time)
+----+-------------+
| 1  | 1448460315  |   //  25/11/2015   -- today
| 2  | 1428440265  |   //  07/04/2015
| 3  | 1418160365  |   //  09/12/2014
| 4  | 1448460215  |   //  25/11/2015   -- today
| 5  | 1438440265  |   //  01/08/2015
| 6  | 1438340265  |   //  31/07/2015
| 7  | 1438437265  |   //  01/08/2015
| 8  | 1448370315  |   //  24/11/2015   -- yesterday
| 9  | 1148370315  |   //  23/05/2006
| 10 | 1447870315  |   //  18/11/2015   -- last week ({11-18}/11/2015)
+----+-------------+

Note: All those number in timestamp column are made of time() function using PHP.

Now I want to know, how can I select all rows which are today Or all rows which are yesterday, or last week?* (it should be noted, in MySQL NOW() is the same with time()).*


For example:

// Times - Today
+----+-------------+
| id |  timestamp  |   // echo date('d/m/Y', $time)
+----+-------------+
| 1  | 1448460315  |   //  25/11/2015   -- today
| 4  | 1448460215  |   //  25/11/2015   -- today
+----+-------------+
Shafizadeh
  • 9,960
  • 12
  • 52
  • 89
  • Store your timestamps as `TIMESTAMP` or `DATETIME` values and use MySQL's built-in [date comparison functions](http://stackoverflow.com/q/3651985/1679849) to do all this work for you instead of faffing about with integers. – r3mainer Nov 25 '15 at 14:37

2 Answers2

1

You would need to use the MySQL BETWEEN function

use PHP to get the timestamp from midnight of day to 11:59:59 of day

date_default_timezone_set('America/Chicago');
$day_begins = strtotime(date('Y-m-d 00:00:00', strtotime('today')));
$day_ends = strtotime(date('Y-m-d 11:59:59', strtotime('today')));


-- sql code will look like
SELECT id FROM table WHERE `timestamp` BETWEEN ($day_begins AND $day_ends)
Kearney Taaffe
  • 647
  • 8
  • 20
  • It is not bad. However I don't know it works or not. But anyway thanks. +1 – Shafizadeh Nov 25 '15 at 14:50
  • are you not wanting the PHP code? You posted this in the PHP section, so, I thought you were wanting how to do this in PHP...if not, my bad. do you have any questions about my code? is there something you don't understand? If I can help you, let me know! – Kearney Taaffe Dec 29 '15 at 08:43
0

With something like:

SELECT
  id,
  timestamp
FROM
  table
WHERE
  DATE_FORMAT(FROM_UNIXTIME(`timestamp`), '%y-%m-%d') = (DATE_FORMAT(NOW(), '%y-%m-%d') - INTERVAL 1 DAY)
KiwiJuicer
  • 1,952
  • 14
  • 28