drupaldrupal-forms

drupal form to calculate date


I need a function to be able to retrieve the selected date from a date_popup in drupal form this is my form and the code that i've tried,Im doing a form for pregnancy calculator and im new in drupal i need to know how to implement the date calculation in my form.As testing i written a function to read value selected but it's giving me the 1970-01-01 date

       <?php

           /*
           *Implementing hook_menu
           */
          function Pregnancy_menu() {
          $items['form/Pregnancy'] = array(
          'title' => 'Pregnancy Calculator',
          'page callback' => 'drupal_get_form',
          'page arguments' => array('Pregnancy_form'),
          'access callback' => TRUE,
          );
         return $items;
          }

      function Pregnancy_form($form_type) {

         $form['Pregnancy_Period'] = array(
         '#type' => 'date_popup',
         '#title' => 'Date of your last menstrual period',
         '#date_format' => 'Y-m-d',
         '#date_text_parts' => array('year'),
         '#date_increment' => 30,
         '#date_year_range' => '-1:0',
         '#default_value' => date(Y) . date(M) . date(D),
         );



          $form['Calculate_Forward'] = array(
          '#type' => 'submit',
          '#value' => t('Calculate Forward'),
            );

       $form['Reset_form'] = array(
       '#type' => 'submit',
       '#value' => t('Reset this Form'),
         );

      $form['Conception_result'] = array(
       '#type' => 'textfield',
        '#title' => t('Conception Occurred:(about two weeks after last menstrual   period)'),     
      '#prefix' => '<div style="float:left;">',
      '#suffix' => '</div>',
      );

  $form['First_trimester_Ends'] = array(
  '#type' => 'textfield',
  '#title' => t('First Trimester Ends :(12 weeks)'),     
  '#prefix' => '<div style="float:left;">',
  '#suffix' => '</div>',
);
$form['Second_trimester_Ends'] = array(
 '#type' => 'textfield',
 '#title' => t('Second Trimester Ends :(27 weeks)'),     
 '#prefix' => '<div style="float:left;">',
 '#suffix' => '</div>',
  );

    $form['Due_Date'] = array(
   '#type' => 'date_popup',
   '#title' => 'Estimated Due Date (40 weeks)',
   '#date_format' => 'Y-m-d',
   '#date_text_parts' => array('year'),
   '#description' => t('Plus or Minus 2 weeks'),
   '#date_increment' => 30,
   '#date_year_range' => '+1:+1',
   '#default_value' => date(Y) . date(M) . date(D),
  );

     $form['Calculate_Backward'] = array(
      '#type' => 'submit',
      '#value' => t('Calculate Backward'),
     );


  return $form;
  }

  function Pregnancy_form_submit($form, &$form_state) { 

//what should I write here to get the value of the selected date and to add on it a specific time.For testing im using:

   $selected_time= $_POST[''Pregnancy_Period'];
   $output=$selected_time;
   drupal_set_message($output);//but it's showing 1970-01-01


}

Solution

  • You don't want to access $_POST variables directly when using Drupal's form API, instead use the $form_state variable, specifically the values key.

    In your submit function, $form_state['values']['Due_Date'] will contain the value inputted in the text box, $form_state['values']['Second_trimester_Ends'] will contain the input for that field, and so on.

    To get the individual date parts you can use:

    list($year, $month, $day) = explode('-', $form_state['values']['Due_Date']);
    

    To add an amount of time to that value use a combination of strtotime and mktime:

    // Prepare a timestamp based on the received date at 00:00:00 hours
    $date = mktime(0, 0, 0, $month, $day, $year);
    $new_date = strtotime('+2 hours', $date);
    $new_date_formatted = date('Y-m-d', $new_date);
    

    There are lots of options for the strtotime function, just have a look a the documentation page linked above.

    Hope that helps