1

I’m trying to get the results of the past 15 days in a database. The time is added in epoch format, like 1400904415.

I’m trying to get only the last results (15 days), so I’m wondering if it is possible to do something like this:

SELECT * FROM dataadded WHERE createdate="here something like createdate its bigger than currentdate - 15 days"

Is that possible?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141

2 Answers2

3

Use DATE_SUB (SUBDATE)

SELECT * FROM dataadded WHERE createdate >= DATE_SUB(CURDATE(), INTERVAL 15 DAY);
danronmoon
  • 3,814
  • 5
  • 34
  • 56
  • Wouldn't you need to wrap the DATE_SUB in UNIX_TIMESTAMP to get a valid comparison? – Devon Bessemer May 28 '14 at 00:58
  • @Devon it is the standard SQL date format, and is implicitly converted to a unix timestamp before the comparison is started. http://dev.mysql.com/doc/refman/5.0/en/using-date.html – danronmoon May 28 '14 at 01:53
3

You can also do this in PHP if you don't want to offload the work to your database

 $date = time() - (86400 * 15); //86400 seconds = 1 day
 $sql = 'SELECT * FROM dataadded WHERE createdate=' . $date;
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • You should have a `>=` instead of a simply an `=` – Martin Konecny May 28 '14 at 00:54
  • Normally I would, but OP didn't use one – Machavity May 28 '14 at 00:55
  • Depending on how accurate of a comparison and where your timestamps are originally coming from, this may be a better solution. If you are filling your database from time() in a PHP script, then this would be the most accurate solution. For readability and taking advantage of MySQL functions, you may consider using DATETIME and NOW() in MySQL for future. – Devon Bessemer May 28 '14 at 01:03