Skip to content

Instantly share code, notes, and snippets.

@mikeselander
Created July 10, 2014 16:44
Show Gist options
  • Save mikeselander/72488098f9cccc9468b5 to your computer and use it in GitHub Desktop.
Save mikeselander/72488098f9cccc9468b5 to your computer and use it in GitHub Desktop.
Gravity Forms doesn't offer a function to export a date field as a UNIX timestamp. This function will find all exported dates in a GF submission & convert it to UNIX, and then update the postmeta to the proper format - as is, it will ONLY work if you're using the Gravity Forms + Custom Post Types plugin to create or edit a post
add_action( 'gform_after_submission', '_convert_date_to_timestamp', 10, 2 ); // Extend
function _convert_date_to_timestamp( $entry, $form ) {
date_default_timezone_set ( "America/Denver" );
// Make sure that we're submitting a post before running the code
if ( $entry['post_id'] ){
foreach ( $entry as $key => $value ){
if ( preg_match( '/(\d{4})-(\d{2})-(\d{2})/', $value ) ) {
// Make sure that we're not overriding the date_created field
if ( $key != 'date_created' ){
// Get the $form fields array id so that we can recover our custom field name
foreach ( $form[fields] as $fields_key => $fields_value ){
if ( $fields_value['id'] == $key ){
$field_id = $fields_key;
} // endif
} // endforeach
$cpt_name = $form[fields][$field_id][postCustomFieldName];
$reformatted_date = date( 'U', strtotime( $value ) );
update_post_meta( $entry['post_id'], $cpt_name, $reformatted_date );
// return to format - $date = date('m-d-Y', $Udate);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment