Modifying date field to show the day [RESOLVED]

I have this snippet that still works on an old installation that changes the date field to display the day:

add_filter( "gform_save_field_value", "transform_date", 10, 4 );

function transform_date( $value, $entry, $field, $form ) {
	// set the form ID and field ID here
	if ( $form[ 'id' ] === 108 && $field[ 'id' ] === 53 ) {
		GFCommon::log_debug( __METHOD__ . "(): Value is {$value}." );
		$timestamp = strtotime( $value );
		GFCommon::log_debug( __METHOD__ . "(): Timestamp is {$timestamp}." );
		$value = date( 'l, M jS', $timestamp );
		GFCommon::log_debug( __METHOD__ . "(): Modified Value is {$value}." );
	}
	// return the value, modified or not
	return $value;
}

This no longer works on the latest version of GF, iv’e tried different methods but cant get a result.

i have played with gform_save_field_value but i’m out of ideas.

I don’t recommend reformatting date field values on save to the entry.

Out of the box, regardless of the format selected in the field settings, Gravity Forms stores all date field values using the Y-m-d format (YYYY-MM-DD). It does this because saving the value in the database consistently enables easier sorting and searching of entries. It also means the format can be changed on the field at any time without impacting existing entries.

Where do you need to display the value in your preferred format?

Hi Richard

I’m saving it to another date field and its purely for visual purposes.

A user selects a date from the date picker, which is then copied to a hidden field using GP Copy Cat. I’d like that field to display the day.

thanks
Mat

Your code includes logging statements, have you tested with logging enabled and checked the core log to confirm Copy Cat actually populated the hidden field?

As long as the hidden field is located after the date field, you could grab the date field value in your code snippet before using strtotime e.g.

if ( empty( $value ) ) {
	$value = rgar( $entry, $date_field_id );
}
$timestamp = strtotime( $value );

Yes the field is being populated.

Logging is enabled and the function is being triggered correctly, this is the log result:

2024-03-26 11:35:36.375098 - DEBUG → transform_date(): Processing Form ID: 108, Field ID: 50 with Value: ‘2024-03-30’.
2024-03-26 11:35:36.375195 - DEBUG → transform_date(): Correct form and field ID matched. Value: ‘2024-03-30’.
2024-03-26 11:35:36.375413 - DEBUG → transform_date(): Timestamp is 1711756800.
2024-03-26 11:35:36.375458 - DEBUG → transform_date(): Modified Value is ‘Saturday, Mar 30th’.
2024-03-26 11:35:36.375499 - DEBUG → transform_date(): Final Value being returned for Form ID: 108, Field ID: 50: ‘Saturday, Mar 30th’.

So its apparently working but the entry is showing //

There are no other conflicting functions, im not sure what else i can check.

Mat

That suggests field 50 is a date type field, which reformats the value for display. Try using an actual hidden type field.

Hi Richard,

Strangely, i tested using a hidden field, and it simply copied the date in the same format DD/MM/YYY, so i gave up…

Coming back to this project today, i submitted another form and Voilà! The date is being formatted! :slight_smile: