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.

Date field to custom post date field problem (from date to unix)

  1. alexktin
    Member

    Good day

    I am trying to submit a date from a form to a custom post type that has a post date field. Unfortunately the date always gets posted as 1/1/1970. I am using the WP Types plugin for the custom posts. When submitting the date through the dashboard everything seems fine, but as soon as I use a from to submit the date it gets posted as 1/1/1970.

    I have read somewhere on the forums that WP Types uses a Unix time stamp and that one has to write a function to convert the date that was entered in the form to a Unix time stamp.

    I have tried different variations of actions and filters to write this function but just cannot get it right. Can someone please be so kind as to assist me in this matter?

    Posted 6 years ago on Friday July 27, 2012 | Permalink
  2. Can you post the code you tried at pastebin.com or pastie.org - we'll be happy to take a look at how you're doing it. Thank you.

    Posted 6 years ago on Sunday July 29, 2012 | Permalink
  3. alexktin
    Member

    Hi Chris

    The code isn't long so I hope you don't mind I decided to paste it here.

    add_action('gform_post_submission_1', 'convert_field_date', 10, 2);

    function convert_field_date($date_field, $converted, $entry){
    $date_field = $entry['5'];

    $converted = strtotime($date_field);
    return $converted;
    }

    Please take a look and let me know if you have any suggestions. Thank you.

    Posted 6 years ago on Friday August 3, 2012 | Permalink
  4. David Peralty
    Administrator

    I see you are using the Post Submission hook. Why is that? Also, where is $converted going once it is returned? Lastly, a date field in Gravity Forms can only accept date strings, so if you try to push a unix time stamp back, it will fail/not-work.

    An input field could take a Unix Time stamp though. So you could have a hidden custom field form field. Grab the date selected on the gform_pre_submission hook, convert it to a time stamp, and then push it to the hidden custom field in your form. Then you would have a custom field with a Unix time stamp.

    Posted 6 years ago on Friday August 3, 2012 | Permalink
  5. David Peralty
    Administrator

    So here is what I did.

    I have a field called Time Stamp. I gave it the CSS class of hidden. I added .hidden { display: none; } to my style.css. I checked off "allow populate dynamically" and just for fun I gave it a parameter of timestamp.

    Here is what I have in my functions.php file

    add_action("gform_pre_submission_30","convert_field_date",10,2);
    function convert_field_date($form){
    $_POST["input_2"] = strtotime($_POST["input_1"]);
    }
    Posted 6 years ago on Friday August 3, 2012 | Permalink
  6. I tried something like this and it doesn't work. For testing purposes, I am trying to send the unix time stamp to the content editor but nothing is showing up.


    add_action("gform_pre_submission_2","convert_field_date");
    function convert_field_date($form){
    $_POST["input_22"] = strtotime($_POST["input_14"]);
    }

    Posted 6 years ago on Thursday January 24, 2013 | Permalink