I have 2 datepicker fields. The first will be titled “Start Date”, and the user will input a date of their choosing. I’d like the second datepicker field to automatically populate with a date 12 months later, rounded to the end of the month. Is this possible? I’m not a programmer, so take it easy on me… Thanks in advance.
Hi John,
Yes, its possible to auto populate the date the date with 12 months from today’s date rounded up to end of the month.
Add the following code in your functions.php file and replace the field ids at the top of the function with actual field ids of your form.
add_action( 'gform_after_submission', 'wpmonks_update_end_date', 10, 2 );
function wpmonks_update_end_date( $entry, $form ) {
// Declare variables for field IDs
$start_date_field_id = 3; // Field ID for the Start Date
$end_date_field_id = 4; // Field ID for the End Date
// Getting the Start Date from the entry
$start_date = rgar( $entry, $start_date_field_id );
// Convert the Start Date to a DateTime object
$start_date_obj = DateTime::createFromFormat('Y-m-d', $start_date);
// Check if the date conversion was successful
if ( $start_date_obj !== false ) {
// Add 12 months to the Start Date
$start_date_obj->modify('+12 months');
// Set the date to the last day of the month
$start_date_obj->modify('last day of this month');
// Update the End Date field in the entry
GFAPI::update_entry_field( $entry['id'], $end_date_field_id, $start_date_obj->format('Y-m-d') );
}
}
Let me know if it worked for you.
Hi John,
You could also check out our Populate Date snippet, to populate the second date field on the frontend, immediately the start date is selected. Here’s tutorial documentation with a link to the snippet and details on to set it up.
Best,
Hello, I am having a similar issue. I have a hidden date field I want to populate with the date 7 days from entry. Can you do this on the page with shortcode or do you have to update the php?
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.