Title: Time Difference calculator
Last modified: May 7, 2025

---

# Time Difference calculator

 *  Resolved [fibbu](https://wordpress.org/support/users/fibbu/)
 * (@fibbu)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/time-difference-calculator/)
 * Hello, I was wondering how I can create an equation like this:
 * I want to have 2 criteria:
    1. **Criterion 1:** Calculate the time difference between two times (HH:mm).
    2. **Criterion 2:** Calculate the time difference between two date+time inputs (
       e.g., DD.MM.YYYY HH:mm).
 * Additionally, depending on the user’s selection:
    - If the user selects **Criterion 1**, only the start and end time inputs should
      appear and be calculated.
    - If the user selects **Criterion 2**, start and end **date+time** inputs should
      appear and be used in the calculation.
 * I’ve been trying some scenarios but haven’t been able to get it to work.
   Would
   you be able to help me?
 * Thank you.
    -  This topic was modified 1 year, 1 month ago by [fibbu](https://wordpress.org/support/users/fibbu/).

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Plugin Author [CodePeople2](https://wordpress.org/support/users/codepeople2/)
 * (@codepeople2)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/time-difference-calculator/#post-18454331)
 * Hello [@fibbu](https://wordpress.org/support/users/fibbu/)
 * You should use conditional statements in the equation to determine the choice
   selected to return the corresponding result.
 * For example, if the date/time fields are fieldname1 and fieldname2, and there
   is a radio button with choices “Criterion 1” and “Criterion 2”. The equation 
   would be simply:
 *     ```wp-block-code
       (function(){  let diff = DATEDIFF(fieldname1, fieldname2, 'mm/dd/yyyy hh:ii', 'y');  if(fieldname3 == 'Criterion 1') {    return diff['hours']+' hours and '+diff['minutes']+' minutes';  }	  if(fieldname3 == 'Criterion 2') {    return diff['years']+' years, '+diff['months']+' months, '+diff['days']+' days, '+diff['hours']+' hours and '+diff['minutes']+' minutes';  }})()
       ```
   
 * And that’s all.
 * Best regards.
 *  Thread Starter [fibbu](https://wordpress.org/support/users/fibbu/)
 * (@fibbu)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/time-difference-calculator/#post-18454358)
 * Thank you, it worked — there’s just one missing part.
 * For example, if the user selects **Criterion 1** and enters:
    - **Start Time:** 14:00
    - **End Time:** 08:00
 * Since the end time is **on the next day**, the actual time difference is **18
   hours**, not 6 hours.
 * How can I structure the logic to handle this correctly — so it automatically 
   assumes the next day if the end time is earlier than the start time?
 * Thank you!
 *  Plugin Author [CodePeople2](https://wordpress.org/support/users/codepeople2/)
 * (@codepeople2)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/time-difference-calculator/#post-18454374)
 * Hello [@fibbu](https://wordpress.org/support/users/fibbu/)
 * In that case you are not ignoring the date component as you describe initially,
   and the diff variable must be calculated into the “if” conditional blocks based
   on the new requirements. Note the last parameter changes:
 *     ```wp-block-code
       (function(){	  if(fieldname3 == 'Criterion 1') {    let diff = DATEDIFF(fieldname1, fieldname2, 'mm/dd/yyyy hh:ii', 'h');    return diff['hours']+' hours and '+diff['minutes']+' minutes';  }	  if(fieldname3 == 'Criterion 2') {    let diff = DATEDIFF(fieldname1, fieldname2, 'mm/dd/yyyy hh:ii', 'y');    return diff['years']+' years, '+diff['months']+' months, '+diff['days']+' days, '+diff['hours']+' hours and '+diff['minutes']+' minutes';  }})()
       ```
   
 * Best regards.
 *  Thread Starter [fibbu](https://wordpress.org/support/users/fibbu/)
 * (@fibbu)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/time-difference-calculator/#post-18454398)
 * [Form Link](http://www.sgkhizmetbilgisi.com/test?cff-form=6)
 * I added the last code you provided.
   I selected **“Kriter A” (Criterion A)** in**
   Fieldname1**, set the **start time to 14:00** and the **end time to 08:00**, 
   but the result shows **6 hours**.It should actually be **18 hours**, since the
   end time is on the next day.
 * Also, it seems like **Criterion B** is not working at all — no result is shown.
 * Could you please help me fix this?
 *  Plugin Author [CodePeople2](https://wordpress.org/support/users/codepeople2/)
 * (@codepeople2)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/time-difference-calculator/#post-18454415)
 * Hello [@fibbu](https://wordpress.org/support/users/fibbu/)
 * If you have date/time components, that’s not possible. Please provide the link
   to the page containing the form.
 * Best regards.
 *  Thread Starter [fibbu](https://wordpress.org/support/users/fibbu/)
 * (@fibbu)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/time-difference-calculator/#post-18454421)
 * The correctly working form is here:
   🔗 [https://onlinealarmkur.com/hours/](https://onlinealarmkur.com/hours/)
 * And the one we built is this:
   🔗 [http://www.sgkhizmetbilgisi.com/test?cff-form=6](http://www.sgkhizmetbilgisi.com/test?cff-form=6)
    -  This reply was modified 1 year, 1 month ago by [fibbu](https://wordpress.org/support/users/fibbu/).
    -  This reply was modified 1 year, 1 month ago by [fibbu](https://wordpress.org/support/users/fibbu/).
 *  Plugin Author [CodePeople2](https://wordpress.org/support/users/codepeople2/)
 * (@codepeople2)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/time-difference-calculator/#post-18454461)
 * Hello [@fibbu](https://wordpress.org/support/users/fibbu/)
 * In your case, where the user cannot access the date component, and the fields
   order is important, you must compare both values, and if the second field’s value
   is lower than the first one, you must increase the second value by one day before
   calculating the difference:
 *     ```wp-block-code
       let d1 = fieldname1, d2 = fieldname2;if(d2<d1){d2 = DATETIMESUM(d2, 'mm/dd/yyyy hh:ii', 1, 'd');}let diff = DATEDIFF(d1, d2, 'mm/dd/yyyy hh:ii', 'h');return diff['hours']+' hours and '+diff['minutes']+' minutes';
       ```
   
 * Please note that the support does not cover implementing the users’ projects.
   If you need personalized technical support, please contact us directly through
   the plugin website. [Contact Us](https://cff.dwbooster.com/contact-us).
 * Best regards.
 *  Thread Starter [fibbu](https://wordpress.org/support/users/fibbu/)
 * (@fibbu)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/time-difference-calculator/#post-18454522)
 * thanks

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Time Difference calculator’ is closed to new replies.

 * ![](https://ps.w.org/calculated-fields-form/assets/icon-256x256.jpg?rev=1734377)
 * [Calculated Fields Form](https://wordpress.org/plugins/calculated-fields-form/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/calculated-fields-form/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/calculated-fields-form/)
 * [Active Topics](https://wordpress.org/support/plugin/calculated-fields-form/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/calculated-fields-form/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/calculated-fields-form/reviews/)

 * 8 replies
 * 2 participants
 * Last reply from: [fibbu](https://wordpress.org/support/users/fibbu/)
 * Last activity: [1 year, 1 month ago](https://wordpress.org/support/topic/time-difference-calculator/#post-18454522)
 * Status: resolved