Display Time Difference in Hours and Minutes

Display calculated time differences in hours & minutes (e.g. 3 hours, 45 minutes).

Instructions

Code

Filename: gpdtc-format-hours-mins.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
 * Gravity Perks // Date Time Calculator // Display Time Difference in Hours and Minutes
 *
 * Instruction Video: https://www.loom.com/share/8498ef1b52334246b654f88d0a3155d4
 *
 * This snippet adds the `gpdtc-format-hours-mins` designator class
 *
 * Plugin Name:  GPDTC Format Results in Hours & Minutes
 * Plugin URI:   https://gravitywiz.com/documentation/gravity-forms-date-time-calculator/
 * Description:  Display calculated time differences in hours & minutes (e.g. 3 hours, 45 minutes).
 * Author:       Gravity Wiz
 * Version:      0.2
 * Author URI:   https://gravitywiz.com
 */
class GPDTC_Format_Hours_Mins {

	public function __construct() {

		// 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() {

		// time for hooks
		add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
		add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );

	}

	public function load_form_script( $form, $is_ajax_enabled ) {

		if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
			add_action( 'wp_footer', array( $this, 'output_script' ) );
			add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
		}

		return $form;
	}

	public function output_script() {
		?>

		<script type="text/javascript">

			( function( $ ) {

				window.GPDTCFormatHoursMins = function( args ) {

					var self = this;

					// copy all args to current object: (list expected props)
					for( var prop in args ) {
						if( args.hasOwnProperty( prop ) ) {
							self[ prop ] = args[ prop ];
						}
					}

					self.init = function() {

						if ( window.gpdtcFormatHoursMinsInitialized ) {
							return;
						}

						gform.addFilter( 'gform_calculation_result', function( result, formulaField, formId, calcObj ) {
							if( self.isApplicableField( formId, formulaField.field_id ) ) {
								var hours = Math.floor( result );
								var diff = hours - result;
								var mins = Math.round( Math.abs( diff * 60 ) );
								result = '{0} hours, {1} minutes'.gformFormat( hours, mins );
							}
							return result;
						} );

						gform.addFilter( 'gform_calculation_format_result', function( formattedResult, result, formulaField, formId, calcObj ) {
							if( self.isApplicableField( formId, formulaField.field_id ) ) {
								formattedResult = result;
							}
							return formattedResult;
						} );

						window.gpdtcFormatHoursMinsInitialized = true;

					};

					self.getField = function( formId, fieldId ) {
						return $( '#input_' + formId + '_' + fieldId ).parents( '.gfield' );
					}

					self.isApplicableField = function( formId, fieldId ) {
						var $field = self.getField( formId, fieldId )
						return $field.hasClass( 'gpdtc-format-hours-mins' );
					}

					self.init();

				}

			} )( jQuery );

		</script>

		<?php
	}

	public function add_init_script( $form ) {

		if ( ! $this->is_applicable_form( $form ) ) {
			return;
		}

		foreach ( $form['fields'] as $field ) {
			if ( $this->is_applicable_field( $field ) ) {
				$script = 'new GPDTCFormatHoursMins();';
				$slug   = implode( '_', array( 'gpdtc_format_hours_mins', $form['id'] ) );
				GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
				break;
			}
		}

	}

	public function format_result( $result, $formula, $field, $form, $entry ) {

		if ( ! $this->is_applicable_field( $field ) ) {
			return $result;
		}

		$hours = intval( $result );
		$diff  = $result - $hours;
		$mins  = round( $diff * 60 );

		return sprintf( '%d hours, %d minutes', $hours, $mins );
	}

	public function is_applicable_form( $form ) {
		foreach ( $form['fields'] as $field ) {
			if ( $this->is_applicable_field( $field ) ) {
				return true;
			}
		}
		return false;
	}

	public function is_applicable_field( $field ) {
		return strpos( $field->cssClass, 'gpdtc-format-hours-mins' ) !== false;
	}

}

# Configuration

new GPDTC_Format_Hours_Mins();

Comments

    1. J Yeager
      J Yeager Staff December 5, 2025 at 2:11 pm

      Hey James,

      Glad to hear that you’re up and running, and that’s a great idea!

      I’ll see what we can do there, and as always, please don’t hesitate to reach out to Support with any other questions in the future.

    1. J Yeager
      J Yeager Staff December 3, 2025 at 4:29 pm

      Hey James,

      Thanks for reaching out!

      I just gave this snippet a quick test by following the Loom on the page, and it seems to be working correctly on my end. I’ll follow up with you via Support so that we can take a look at what’s going wrong here for you.

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.