Format Date & Time Merge Tags
Adds merge tag modifiers for formatting date and time merge tags using PHP Date Formats.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Read the Walkthrough
Code
Filename: gw-format-date-merge-tags.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* Gravity Wiz // Gravity Forms // Format Date & Time Merge Tags
* https://gravitywiz.com/gravity-forms-date-merge-tags/
*
* Adds merge tag modifiers for formatting date and time merge tags using PHP Date Formats.
*
* Plugin Name: Gravity Forms — Format Date & Time Merge Tags
* Plugin URI: https://gravitywiz.com/gravity-forms-date-merge-tags/
* Description: Adds merge tag modifiers for formatting date and time merge tags using PHP Date Formats.
* Author: Gravity Wiz
* Version: 0.6
* Author URI: https://gravitywiz.com
*/
class GW_Format_Date_Merge_Tag {
private $_args = array();
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'form_id' => false,
'field_id' => false,
'locale' => null,
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
add_filter( 'gform_pre_replace_merge_tags', array( $this, 'replace_merge_tags' ), 10, 7 );
}
public function replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) {
if ( ! $this->is_applicable_form( $form ) ) {
return $text;
}
$current_locale = determine_locale();
$locale = $this->_args['locale'];
if ( $locale ) {
switch_to_locale( $locale );
}
preg_match_all( '/{[^{]*?:(\d+(\.\d+)?)(:(.*?))?}/mi', $text, $matches, PREG_SET_ORDER );
foreach ( $matches as $match ) {
$input_id = $match[1];
$field = GFFormsModel::get_field( $form, $input_id );
// ✅ Minimal change: allow date AND time fields
if ( ! $field || ! in_array( $field->get_input_type(), array( 'date', 'time' ), true ) ) {
continue;
}
$i = $match[0][0] === '{' ? 4 : 5;
$modifier = rgar( array_map( 'trim', explode( ',', rgar( $match, $i ) ) ), 0 );
if ( ! $modifier ) {
continue;
}
$value = GFFormsModel::get_lead_field_value( $entry, $field );
$value = $field->get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $value, $url_encode, $esc_html, $format, $nl2br );
if ( ! $value ) {
continue;
}
if ( $field->get_input_type() === 'date' ) {
$format = $field->dateFormat ? $field->dateFormat : 'mdy';
$parsed_date = GFCommon::parse_date( $value, $format );
$timestamp = strtotime(
sprintf(
'%d-%d-%d',
$parsed_date['year'],
$parsed_date['month'],
$parsed_date['day']
)
);
} else { // time field
// Handles formats like "2:30 pm" or "14:30"
$timestamp = strtotime( $value );
}
// On the Notifications/Confirmation side, & gets encoded to &. Decode it back.
$modifier = htmlspecialchars_decode( $modifier );
// For whatever reason, Populate Anything's LMTs works better with `&comma` than `,`. But... date() doesn't
// like it so let's replace it before we pass it to date().
$modifier = str_replace( ',', ',', $modifier );
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$replace = wp_date( $modifier, $timestamp, new DateTimeZone( 'UTC' ) );
$text = str_replace( $match[0], $replace, $text );
}
// Switch back to default locale.
if ( $locale ) {
switch_to_locale( $current_locale );
}
return $text;
}
public function is_applicable_form( $form ) {
$form_id = isset( $form['id'] ) ? $form['id'] : $form;
return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id'];
}
}
# Configuration
new GW_Format_Date_Merge_Tag();
# Apply locale to all forms.
//new GW_Format_Date_Merge_Tag( array(
// 'locale' => 'fr_FR',
//) );
# Apply locale to a specific form.
//new GW_Format_Date_Merge_Tag( array(
// 'form_id' => 123,
// 'locale' => 'fr_FR',
//) );