The built-in Gravity Forms merge tags include {date_mdy}
and {date_dmy}
, which are useful for setting the default values of Date fields, but there aren’t any for Time fields. This snippet adds three merge tags for that:
{current_hour}
{current_minute}
{current_am_pm}
Note that the server’s clock and site’s timezone setting are used, not the client’s.
add_filter( 'gform_replace_merge_tags', 'djb_gform_replace_merge_tags', 10, 7 ); /** * Replace custom merge tags. * * @link https://docs.gravityforms.com/gform_replace_merge_tags/ * * @param string $text Current text in which merge tags are being replaced. * @param object $form Current Form object. * @param object $entry Current Entry object. * @param boolean $url_encode Whether or not to encode any URLs found in the replaced value. * @param boolean $esc_html Whether or not to encode HTML found in the replaced value. * @param boolean $nl2br Whether or not to convert newlines to break tags. * @param string $format Determines how the value should be formatted. Default is html. * @return string Modified data. */ function djb_gform_replace_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) { if ( strpos( $text, '{current_hour}' ) !== false ) { $text = str_replace( '{current_hour}', current_time( 'g' ), $text ); } if ( strpos( $text, '{current_minute}' ) !== false ) { $text = str_replace( '{current_minute}', current_time( 'i' ), $text ); } if ( strpos( $text, '{current_am_pm}' ) !== false ) { $text = str_replace( '{current_am_pm}', current_time( 'A' ), $text ); } return $text; }
hi
how can i add this code to gravity form?
See https://docs.gravityforms.com/where-do-i-put-this-code/.
Thank you very much for this snippet – it has been invaluable! I had to change the g to a H to format it the way I needed: https://codex.wordpress.org/Formatting_Date_and_Time but this has been really useful thank you!
This was exactly what I needed! Thanks so much! And thanks for the pointers into the documentation. I looked for this but missed it.
Hi,
Thank you so much for this snippet!
How difficult would it be to show the hours in the 24hour format?
Thanks in advance!
Replace ‘g’ with ‘G’ in the
current_hour
statement, and remove thecurrent_am_pm
statement.WOW …. Thank you sooooooo much…. Still works perfectly…
i am looking for a solution to make conditional logic depends the real time also submit the form after a specific time. this for a time based survey. Anybody can help me in this regard.
For conditional logic based on time, see https://gravitywiz.com/setup-time-based-conditional-logic-gravity-forms/. To schedule the entire form, go to Settings > Form Settings > Restrictions > Schedule form.
I have created a simple time clock. Using the script above, I am populating a date field (military time, hiding the AM/PM via CSS). The script works about 50% of the time. Other times, it populates the field with a recent time from a previous form entry. Example:
1. User “clocks in” at 10:30 (correct time).
2. Next user clocks in at 11:00 (correct time), but the time field reads “10:30” and 10:30 is recorded in the entry.
3. Subsequent users see 10:30 as the populated time regardless of the actual time, although not all. (I see the correct time from my PC every time.)
4. After an undetermined period, the auto-populated time seems to correct itself.
If I delete the original entry with “10:30” as the populated time, next users will see the correct time in the field. So a previous entry seems to be the source of the incorrect time, but I cannot figure out why.
The time clock can be found at https://verifyi9.com/time-clock/
Are you running any caching plugins? Do you still see the correct time every time if you’re logged out?
Hi Dylan, I had forgotten that my site host provides caching. I had them exclude the form page from caching and the process now works correctly. Thank you for pointing me in the right direction.
How would one do this with seconds?
Adding a {current_second} merge tag would be easy, but there’s no seconds subfield in the Gravity Forms Time field (it’s an open enhancement request).
Thanks!! Still works
Time saver 🙂