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.

gform_save_field_value datepicker errors

  1. reddishpink
    Member

    Hello. I'm trying to use gform_save_field_value to save a date from a datepicker field (formatted d/m/Y) into iso format (Y-m-d). My original code is:

    add_filter("gform_save_field_value", "save_iso_date", 2, 9);
    function save_iso_date($value, $lead, $field, $form) {
    list($day, $month, $year) = explode("/", $value);
    return sprintf('%s-%s-%s', $year, $month, $day);
    }

    So, for a date given in the datepicker as 06/05/2013, this adds the value in the database as '--2013-05-06' for instance. If I replace '%s-%s-%s' with something like '%s/%s/%s' it results in '//2013-05-06'. If I drop the first two characters like so:

    return substr(sprintf('%s-%s-%s', $year, $month, $day), 2);

    then what's added to the db is the original string 06/05/2013. Something weird is happening. Am I going about this the right way?

    Posted 10 years ago on Monday May 27, 2013 | Permalink
  2. David Peralty

    You can't just change our date field to save data in a different way than we've programmed it.

    If you want to do this, then your best bet is to use a regular input field, add a CSS class to it, and manually hook the JQuery datepicker script to that field. Then you can have full control over how the date is saved in the database.

    Posted 10 years ago on Monday May 27, 2013 | Permalink
  3. reddishpink
    Member

    Oh, okay. That's a bit frustrating since it makes the dates unsortable in the database when the date field is used. Thanks for your reply.

    Posted 10 years ago on Tuesday June 4, 2013 | Permalink
  4. David Peralty

    Sorry about that. All my best!

    Posted 10 years ago on Tuesday June 4, 2013 | Permalink