Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6625181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:44:34+00:00 2026-05-25T21:44:34+00:00

I need a function to be able to retrieve the selected date from a

  • 0

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


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:44:35+00:00Added an answer on May 25, 2026 at 9:44 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to be able to write a function that shows repeated words from
By using the function Date(ymdHis) i am able to retrieve the value of the
We need to make our enterprise ASP.NET/NHibernate browser-based application able to function when connected
I need to be able to interrogate a function/method and get a list of
Need a function that takes a character as a parameter and returns true if
Need a function like: function isGoogleURL(url) { ... } that returns true iff URL
I need a function count_permutations() that returns the number of permutations of a given
I need a function that I can call when somthing is done, for example
I need to retrieve an array with category names. It seems that tag names
I have a multidimensional array. I need a function that checks if a specified

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.