I have a custom search form that I posts a year + month, such as 2014-04 I would like to get all nodes created that month, when I create the timestmap I use: "SELECT ....... AND changed >= 'strtotime($date)'"

This is the code for form date:

$form['aqar_date'] = array(
'#type' => 'date_select', 
'#title' => t('Date'),
'#prefix' => '<div id="space_replace" class="search-title-date">   <h2>Node date</h2>',
'#suffix' => '</div>',     
'#date_format' => 'Y-m',
'#default_value' => date('Y-m'),
'#date_year_range' => '-4:+0', 

This is the form submit:

$start_date = strtotime($date);
$query = "
SELECT `nid` FROM `node`
`type` = 'custom'
 AND `changed` >= '$start_date'";

But I'm not getting, I need a way to specify to only this month period not this month and newer!


  • 2
    Welcome to Drupal Answers! May you show the actual code you are using? – kiamlaluno Apr 30 '14 at 12:20
  • @kiamlaluno yep sure I just did, any thoughts? – Amin Apr 30 '14 at 12:40
  • What's in $date? – Clive Apr 30 '14 at 12:44
  • @Clive it's actually the date submitted by the form, "2014-04" for instance, it is in the format of 'Y-m' – Amin Apr 30 '14 at 12:56
  • But how are you setting $date? With $date = $form_state['values']['aqar_date']; or something else? – Clive Apr 30 '14 at 13:18

Here you go with the code.. I have used entity field query to retrieve results.. Code should be self explanatory.. Let me know if you have any difficulty in understanding below code..


$year_month = "2014-04";
$date = date_parse_from_format ("Y-m" ,  $year_month);
$num = cal_days_in_month(CAL_GREGORIAN, $date['month'], $date['year']);
$first_minute = mktime(0, 0, 0, $date['month'], 1, $date['year']);
$last_minute = mktime(23, 59, 59, $date['month'], $num, $date['year']);
$entity_type = 'node';
$efq = new EntityFieldQuery();
$efq->entityCondition('entity_type', $entity_type);
$efq->propertyCondition('created', array($first_minute, $last_minute), 'BETWEEN');
$result = $efq->execute();  

|improve this answer|||||
  • Thanks that did the job, though it was hard for me to come up with that it looks shorter than I anticipated :) – Amin Apr 30 '14 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.