PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Calculate Time Difference

  1. In my form, I have a start time and end time. I would like to have a dynamically calculated field that contains the length of time from start time to end time.

    I can't seem to find anything like this already in the forums or documentation, and the calculation field seems to exclude the time fields.

    Posted 11 years ago on Monday July 23, 2012 | Permalink
  2. Does the length of time need to be available in the form before submission for a calculation, or does it just need to be stored with the entry?

    Posted 11 years ago on Monday July 23, 2012 | Permalink
  3. Just stored with the entry so it's available in the results/export.

    Posted 11 years ago on Monday July 23, 2012 | Permalink
  4. David Peralty

    To do that, you'll need to do some PHP coding.

    Create a field called length.
    Check off it being allowed to be populated dynamically
    Give it a CSS class of hidden
    In your style.css add:

    .hidden { display: none; }

    In your functions.php file you will want to use the following hook:
    http://www.gravityhelp.com/documentation/page/Gform_pre_submission

    And then look at the following information:
    http://php.net/manual/en/function.date.php

    http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php

    At the end, you should have an invisible form field that upon submission of the form gets the value of the difference between the two dates.

    Posted 11 years ago on Monday July 23, 2012 | Permalink
  5. Couldn't get back to this for a bit, but if anyone else is interested, here's the code I through together successfully....

    http://pastebin.com/hySGFnLt

    Posted 11 years ago on Monday August 6, 2012 | Permalink
  6. David Peralty

    Beautiful code. Good job.

    Posted 11 years ago on Monday August 6, 2012 | Permalink

This topic has been resolved and has been closed to new replies.