Calculate time between start and end time
There is a way to accomplish this without writing any code. You can use the Rules module to calculate the difference between dates and to update a field in any entity or node. 

There isn't a Rules action that will calculate the difference between dates, but there is still a way to do it in Rules. It involves four simple steps:


Use the "Convert data type" action to convert both date objects to Integer types. This will create variables that use a Unix int timestamp (number of seconds since 1/1/1970)
Calculate the difference between the two integers and store in a variable
Divide the result by x number of seconds, depending on what you want to calculate. To calculate days, divide by 86400 (60 * 60 * 24)
Use "Convert data type" to round the subtraction to an integer (and store in a new variable)


Then you can simply use the Rules action "Set a data value" to store this value in a node (or entity) field

You can trigger this Rule whenever the user saves or edits content of specific type.