You are here

21 August 2012

This query gets node created dates and selects all years of nodes that has been made. Also there are filters: node.status equals 1 is published filter, node.type is content type filter and node language is language filter which I have set to current language.

Source code viewer
  1. global $language;
  2. $result = db_query("SELECT EXTRACT(YEAR FROM((DATE_ADD('19700101', INTERVAL node.created SECOND) + INTERVAL 10800 SECOND))) AS created_year, COUNT(node.nid) AS num_records FROM {node} node WHERE (( (node.status = '1') AND (node.type IN ('article')) AND (node.language IN (:lang)) )) GROUP BY created_year ORDER BY created_year DESC", array(':lang' => $language->language));
  3. if ($result->rowCount() > 0) {
  4. foreach($result as $row) {
  5. echo $row->created_year;
  6. }
  7. }
Programming Language: PHP