Adding some time to a date

Hi,
I have a form with a date field for when a process is supposed to happen and then a radio button field with a selection of 1,2,3 or 6 months for a second process to happen.
What would make things easier for me is to have a 2nd date field automatically take the first date and add the selected number of months.
e.g. if process 1 is set for the 20th of January 2022 and the radio is set for 2 months then process 2 should be set to the 20th of march 2022.
The second date field doesn’t have to be shown/updated to the user but I would need it updated pre submission.
I was thinking gform_pre_submission would be the way but i’m not good with PHP and don’t know how to calculate a date + X months.
Thanks for any help

Hello. The simplest way is with this perk from Gravity Wiz:

Without that, you will need to become familiar with PHP. Using gform_pre_submission will work OK if you only need to store that date in the future in the entry, and that does not need to be seen in the form before it is submitted.

Hi, Thanks for the reply.
Unfortunately I don’t have access to Gravity perks. They have a lot of perks i’d like to use.

The entry needs to be stored but does not need to be displayed before submission.
Wish I had more experience with code.

There is an example for working out the age which i think would cover the core of what i need. I just need to work out the code to add x months (from another fields value) to the date

add_action( 'gform_pre_submission_5', function ( $form ) {
    // Get the date field.
    $date_field_id = '10';
    $date_field    = GFAPI::get_field( $form, $date_field_id );
 
    // Get the date field value.
    $value      = $date_field->get_value_submission( array() );
    $date_value = GFFormsModel::prepare_date( $date_field->dateFormat, $value );
 
    // Get the second field value for the number of months to add

    // Add the number of months to the date value
 
    // Populate field 11 with second date.
    $_POST['input_11'] = $date2->y;
} );

It looks like field 10 is your date field. Field 11 is where you want to store the date with X months added. What is the ID of the radio button field where you have 1, 2, 3, or 6 choices?

And does that field look like this (with the “show values” option checked):

Or something different?

To be honest, that bit of code was roughly copied from the documentation examples here

My first date is, by chance, ID 10
My number of months to add is ID 16 is a radio button and yes, has values shown/set
My second date is ID 22 and is hidden
Both ID 10 and 22 are currently date picker format and dd/mm/yyyy.

Here is my guess at the core of the PHP. still haven’t found any example code to add the months to the date yet.

add_action( 'gform_pre_submission_13', function ( $form ) {
    // Get the date field.
    $date_field_id = '10';
    $date_field    = GFAPI::get_field( $form, $date_field_id );
 
    // Get the date field value.
    $value      = $date_field->get_value_submission( array() );
    $date_value = GFFormsModel::prepare_date( $date_field->dateFormat, $value );
 
    // Get the number of months to add field  -  a radio button with single didgit values
    $month_field_id = '16';
    $months_field   = GFAPI::get_field( $form, $months_field_id );

    // Get the number of months value.
    $M_value  = $months_field->get_value_submission( array() );
    $months_value   = GFFormsModel::prepare_date( $months_field->numberFormat, $M_value );
    // the number format above is a guess

    // Add the number of months to the date value
    $date2 = $date_value + $months_value(in months)
    // the above is just a place holder for what needs to be done
 
    // Populate field 22 with second date.
    $_POST['input_22'] = $date2->y;
} );

Hi again, for the adding months to the date area does the below look correct?

    $date2 = $date_value->modify('+months_value month')
    $date2 = $date2->dateformat

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.