Date field with formate DDMMYYYY -

Hi,
I need a date field in my form that should

  1. be automatically filled with the current date (today)
  2. Has the format DDMMYYY, without any slashes / hyphens or other characters. e.g. 04062020
  3. Is visible for the user but can not be edited.

How can I achieve that?

If this can not be done out of the box, could you post a little wordpres plugin code snipped for me?
This is needed for a non-profit project, working with refugees - your help would be appreciated very much.
Cheers,
Matt

Hi Matt, it’s not too hard to accomplish that. First, add a single-line text field to your form. On the Appearance tab, give it a Custom CSS class name of gf_readonly and on the Advanced tab, check the box “allow field to be populated automatically” and add the parameter name of ‘date’.

Then add this code to your theme functions.php file:

// update '1' to the ID of your form
add_filter( 'gform_pre_render_1', 'add_readonly_script' );
function add_readonly_script( $form ) {
    ?>
 
    <script type="text/javascript">
        jQuery(document).ready(function(){
            /* apply only to a input with a class of gf_readonly */
            jQuery("li.gf_readonly input").attr("readonly","readonly");
        });
    </script>
 
    <?php
    return $form;
}

Be sure to update the form ID from 1 to whatever your form ID is, or remove the _1 from the filter if you want to have this code available on any form.

Those two steps will give you a field to hold the data, and make the input appear read only. If you want to style that readonly field, please see the advice here:

Go to the “Styling the Fields” section.

The last part is to populate the field with the date when the form is shown. You already gave the text field the parameter name of ‘date’, so you can use this filter to populate the field with a date:

The actual code for you for the date format DDMMYYYY will look like this:

add_filter( 'gform_field_value_date', 'populate_the_date' );
function populate_the_date( $value ) {
	// desired format DDMMYYYY
	// ref https://www.php.net/manual/en/function.date.php
	$local_timestamp = GFCommon::get_local_timestamp( time() );
	return date_i18n( 'dmy', $local_timestamp, true );
}

That code can go into the theme functions.php file right near the gform_pre_render code you added for the readonly script.

If you have any questions, please let us know.

1 Like

Hi Chris, thanks for your help!
Based on your code snippets I created two little WordpresPlugins that can be easily installed. For anyone who has the same or similar requirements in the future I uploaded them to GitHub:

Since I am not a developer, let me know if you find any mistakes.
Cheers,
Matt

ah, ok I found one mistake:

$local_timestamp = GFCommon::get_local_timestamp( time() );

does not return my current date, but yesterdays date. I Guess this happens because of issues with the time zone. Is there an easy way how to make sure that the form users current time zone is used?
Thanks!

© 2008 - 2021. Gravity Forms is a project by Rocketgenius Inc.