drupal形式计算日期

发布于 2024-12-06 07:47:33 字数 2809 浏览 9 评论 0原文

我需要一个函数能够从 drupal 形式的 date_popup 中检索选定的日期,这是我的表单和我尝试过的代码,我正在为怀孕计算器做一个表单,我是 drupal 的新手,我需要知道如何实现在我的表单中进行日期计算。作为测试,我编写了一个函数来读取所选值,但它给了我 1970-01-01 日期

       <?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) { 

//我应该在这里写什么来获取所选日期的值并在其上添加特定的值时间。为了测试我使用:

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


}

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


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡水深流 2024-12-13 07:47:33

使用 Drupal 的表单 API 时,您不想直接访问 $_POST 变量,而是使用 $form_state 变量,特别是 values 键。

在您的提交函数中,$form_state['values']['Due_Date'] 将包含在文本框中输入的值,$form_state['values']['Second_trimester_Ends']< /code> 将包含该字段的输入,依此类推。

要获取可以使用的各个日期部分:

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

要向该值添加时间量,请使用 strtotimemktime

// 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);

有很多选项strtotime 函数,只需查看上面链接的文档页面即可。

希望有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文