views:

88

answers:

2

I'm trying to create a custom query that will show the number of stories that have been posted in the last 24 hours on a Drupal 6 site.

Stories are stored in the "node" table. each record has a "created" row that records the UNIX timestamp when the story was posted.

Here's the query I'm trying so far:

$sq = 'SELECT COUNT(*) cnt '
    . 'FROM {node} c WHERE created >= dateadd(hour,-24,getdate())'; 

This doesn't appear to be working though. What am I doing wrong?

EDIT: Here's the overall code I'm trying to use right now:

$sq = 'SELECT COUNT(*) AS cnt FROM {NODE} n WHERE FROM_UNIXTIME(n.created) >= DATE_SUB(NOW(), INTERVAL 1 DAY)';                                                                                        

$q = db_query($sq);

while ($o = db_fetch_object($q)) {

            print_r($o);

}

That print_r isn't returning anything. Where's my error?

+4  A: 

For MySQL, use:

SELECT COUNT(*) AS cnt
  FROM NODE n
 WHERE FROM_UNIXTIME(n.created) >= DATE_SUB(NOW(), INTERVAL 1 DAY)

Mind that NOW() includes the time when the statement is run. If you want to count records, starting from midnight of the previous day, use:

SELECT COUNT(*) AS cnt
  FROM NODE n
 WHERE FROM_UNIXTIME(n.created) >= DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY)

Reference:

OMG Ponies
Alternatively, wrap the `date_sub(...)` in `UNIX_TIMESTAMP()`
Phil Brown
Hmm, I'm goofing this up: <br/>$sq = 'SELECT COUNT(*) AS cnt FROM {NODE} n WHERE FROM_UNIXTIME(n.created) >= DATE_SUB(NOW(), INTERVAL 1 DAY)'; $q = db_query($sq);$stats = array();while ($o = db_fetch_object($q)) { $stats[$o->day] = array($o->day, $o->cnt); print_r($o); }
bflora
@bflora: Why the curly brackets on the table name? You might need to enclose it in backticks -- `{node}`
OMG Ponies
Just posted an edit to the question with the overall code I'm trying to use to test this out. The print_r statement isn't returning anything....
bflora
@bflora: Thx for the question update, sorry but I have to turn in but I don't know Drupal API, just SQL :/
OMG Ponies
But `FROM_UNIXTIME` kills all the performance :-S Why don't just wrap the right operand with another `TIMESTAMP()`? Also - `NOW() - 86400` can be enough precise here ;-)
zerkms
Like this? $sq = 'SELECT COUNT(*) AS cnt FROM NODE n WHERE (n.created) >= (NOW() - 86400)';
bflora
+1 This is a good answer using only SQL, but not the best to use with Drupal though. On a side note, curly brackets are used to make it possible to use table prefixes without putting them into the written SQL. That's part of Drupal's query wrapper function.
googletorp
+2  A: 

Since you are doing this in PHP, you can just use $_SERVER['REQUEST_TIME']. My guess is that it will be faster than doing date manipulations with SQL:

$count = db_result(db_query("SELECT COUNT(nid) FROM {node}
          WHERE created >= %d;", $_SERVER['REQUEST_TIME'] - 86400));

Alternative you could use time to get the current timestamp, but that will be a tiny bit slower than using the $_SERVER['REQUEST_TIME'] variable.

googletorp