Show day of week in notification [RESOLVED]

I’ve used a date-field in a form. In the conformation mail and notifications, I would like to display the day of week before the date, for example: Monday 19-05-2020. Is there a way to do this?

Hi Willem. You can do this with a small bit of code added to your theme functions.php file. Here’s the code:

add_filter( 'gform_merge_tag_filter', 'add_day_of_week', 10, 6 );
function add_day_of_week( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
  // if it's the all_fields merge tag, and field ID2, and the modifier is dow i.e. {all_fields:dow}
  if ( $merge_tag == 'all_fields' && $field->id == '2' && $modifier == 'dow' ) {
    // figure out the day from the date
    $timestamp = strtotime( $raw_value );
    $value = date( 'l', $timestamp ) . ' ' . $value;
  }
  // return the unmodified or modified value
  return $value;
}

Change the field->id from 2 to whatever your date field is.

Then, for the all_fields merge tag in your notification, add the modifier :dow like this:

{all_fields:dow}

That will output the day of week, with a space, and then your formatted date. Let me know if you need any assistance with that.

Thanks for this excellent solution. It works!

But, the day of the week is displayed in English. I would like to translate this to Dutch. Could you tell me how to achieve this?

Thanks in advance,

What if you have a specific field in a formatted notification (not using all fields, but inserting fields into a template I created)? I tried…

add_filter( 'gform_merge_tag_filter', 'add_day_of_week', 10, 6 );
function add_day_of_week( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
  // if it's the all_fields merge tag, and field ID2, and the modifier is dow i.e. {all_fields:dow}
  if ( $merge_tag == 'Date of Service' && $field->id == '4' && $modifier == 'dow' ) {
    // figure out the day from the date
    $timestamp = strtotime( $raw_value );
    $value = date( 'l', $timestamp ) . ' ' . $value;
  }
  // return the unmodified or modified value
  return $value;
}
``` and tried both {Date of Service:dow} and {Date of Service:4:dow} but can't get the day of week to show. Ideally it would format something like Saturday December 11, 2021.

Thanks

That conditional wouldn’t work, I believe when filtering a field specific merge tag the $merge_tag variable will not be the field’s label, it will be the field ID so the condition will never be met. If you’re using {Date of Service:4:dow} $merge_tag will be 4, not “Date of Service”.

You’ll also likely get a better result if you just target all date fields that have the :dow modifier as well to use with other forms and date fields across your site without having to update the ID each time in the code.

add_filter( 'gform_merge_tag_filter', 'add_day_of_week', 10, 6 );
function add_day_of_week( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
    // if it's the all_fields merge tag, and field ID2, and the modifier is dow i.e. {all_fields:dow}
    if ( $field->type == 'date' && $modifier == 'dow' ) {
        // figure out the day from the date
        $timestamp = strtotime( $raw_value );
        $value = date( 'l F j, Y', $timestamp );
    }
    // return the unmodified or modified value
    return $value;
}

The above grabs any date field’s value where you’ve added the :dow modifier and converts it to the format you’re looking for.

Merge tags in use, using the same date field merge tag with and without the :dow modifier:

Merge tags after being replaced:

1 Like