Drupal SQL query to fetch single node created on current date
I have a project where I need to create a single开发者_如何学编程 node of a certain type for each day. How does my custom SQL query need to be formatted so that I retrieve one node data from the database where the node date matches the current date?
I am having trouble because the node timestamp in the database contains date and time info. So, I can't do a straight equality check because I can only check against the current date timestamp.
Basic example for D6. You can do something like:
<?php
$start = date('U', mktime(0, 0, 0, date('n'), date('j'), date('Y')));
$end = date('U', mktime(23, 59, 59, date('n'), date('j'), date('Y')));
// Database query to retrieve the node's nid
$nid = db_result(db_query('SELECT nid FROM {node} WHERE created BETWEEN %d AND %d', $start, $end));
// Load the found node
$node = node_load($nid);
?>
If your node is really simple (just title and body, etc like the basic "page" and "story" types) instead of getting the nid, could just use db_query()
to get all of the info right from the node table.
精彩评论