PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Dynamically Populating Date Field

  1. theslink2000
    Member

    Hi,

    I'm trying to dynamically populate a date field so that the user can see the existing entry and then update it using the date field (with date picker) as normal.

    This process has not been a problem for every other field type I've tried and I'm sure it's not for the Date Field, however I can populate the field but the field isn't interpreting the data as I expected. I've used a date field to save the data (in this case the original date is 08/12/1965 and that's how it outputs around the site, but the database stores it as -128304000. Obviously this must be correct as everything else seems to understand it, so I expected when I passed it back to the Date Field as a string it would understand it and convert it, but it doesn't it just displays -128304000.

    I can find no mention of this in the documentation or elsewhere in these forums, could someone help me interpret this please?

    Here's my code, as you can see I've tried using the php date() function, but to no effect:

    add_filter('gform_field_value_original_dob', 'original_dob');  //Dynamically populate the original dob field
    //Populate the original date of birth edit field
    function original_dob() {
    	$post_id = $_GET['original_post_id'];  //Pass GET function to pull variable from query string
    	$post_field = "-date-of-birth";  //Supply the bit of the field name specific to this item
    	$data = populate_edit_field($post_id, $post_field);  //Build full funtion to find the required item of data
    	$date = date('d/m/Y', $data);
    	return $date;
    }
    
    //Function to build the command to and find the field data
    function populate_edit_field ($post_id, $post_field) {
    	$post_type = get_post_type($post_id);  //Use get_post_type to find the type of post, this is used as the part of the field name that changes for each type, such as "wpcf-surgery-street-address"
    	$database_field = "wpcf-" . $post_type . $post_field;  //Build complete field name
    	$post_data = find_post_data($post_id, $database_field, true);  //Finally find the contents of the field and post it back to the form
    	return $post_data;
    }
    
    //Retrieve post meta based on the post id passed via query string to the form
    function find_post_data($postid, $fieldname, $returntype) {
    	global $post;  //Call the global $post variable to access stored data
    	$value = get_post_meta($postid, $fieldname, $returntype);  //Return the contents of the requested field for the passed post id
    	return $value;
    }
    
    add_action("gform_after_submission_26", "update_dob", 10, 2);  //Update the edited date of birth in the database
    //Update the existing monitor dosage system type post on submitting the form
    function update_dob($entry) {
    	 update_post_meta($entry[1], "wpcf-patient-date-of-birth", $entry[2]);  //Update the post (stored in field 1), field name and the new value (stored in field 3)
    }
    Posted 8 years ago on Saturday August 25, 2012 | Permalink
  2. theslink2000
    Member

    I've worked it out, there was a syntax error, my bad! I tried for hours before posting here, sorry.

    For anyone wondering Gravity Forms and I'm assuming Wordpress and PHP and SQL in general store dates as timestamps. This is what threw me as the time stamp is the number of seconds since 00:00 01/01/1970 the timestamp was created. Sounds weird but that's what date() does, it decodes this.

    To complete my editing code I needed to modify the after_submission function to this:

    //Update the existing monitor dosage system type post on submitting the form
    function update_dob($entry) {
    	 $timestamp = strtotime($entry[2]);
    	 update_post_meta($entry[1], "wpcf-patient-date-of-birth", $timestamp);  //Update the post (stored in field 1), field name and the new value (stored in field 3)
    }

    Note the use of the strtotime() function to create the new timestamp. Once this is done it's working beautifully.

    The above code is now correct and is working for me. It's up to the admins if they want to delete this thread or not as I've sorted myself out, but I couldn't find this in the documentation so it may prove useful.

    Posted 8 years ago on Saturday August 25, 2012 | Permalink
  3. We'll leave it open. Thanks for posting how you resolved your problem.

    Posted 8 years ago on Sunday August 26, 2012 | Permalink
  4. theslink2000
    Member

    No worries bud. Hope someone finds it useful.

    Posted 8 years ago on Sunday August 26, 2012 | Permalink