Calculate time between start and end time I'm building a website where I need to know the time (in hours) between the start and end time from a date field. So I want to know how long it took. It's usually from one day to another, sometimes just on one day (don't know if that matters).

I made a field Date (Unix timestamp) with month, day, hour and minute. Users need to choose both start and end time.
I also made a Computed field. The first part is the Computed code and the second part is de display code:

&lt;?php
$field_tijden = array_pop(field_get_items($entity_type, $entity, 'field_date'));

$start = $field_tijden['value'];
$end = $field_tijden['value2'];
$timezone = $field_tijden['timezone'];

$date_start = new DateTime($start, $timezone);
$date_end = new DateTime($end, $timezone);

$interval = $date_start-&gt;diff($date_end);

$entity_field[0]['value'] = $interval-&gt;format('%h');
?&gt;



&lt;?php
$display_output = $entity_field_item['value'] . " hours";
?&gt;


I checked "Store value in the database", checked "Float", data length "3,2" and the other settings I kept on default. This is what I copied from another website and all I tweaked was changing it to $field_tijden which is the name of the field.
I know almost nothing about PHP, so I have no idea what to change to get this to work. Right now when I save a node the field is not being displayed.

Is there anyone who can help me getting this to work?
Thanks in advance!

Edit: These are the settings I put in a Rule, but it's not working yet. Does anyone see what I'm doing wrong?

Events:
 - After saving node


After updating existing node


Conditions: nothing

Actions:


Convert data type. Value: Integer. Selector: node:field-tijden:value (this is the start time). Label: conversion-result-start.
Convert data type. Value: Integer. Selector: node:field-tijden:value2 (this is the end time). Label: conversion-result-end.
Calculate a value. Selector: conversion-result-end. Operator value: (-). Selector 2: conversion-result-start. Label: result-calculation.
Calculate a value. Selector: conversion-result-calculation. Operator value: (*). Selector 2: 60. Label: result-in-minutes.
Calculate a value. Selector: conversion-result-in-minutes. Operator value: (*). Selector 2: 60. Label: result-in-hours.
Convert data type. Value: Integer. Selector: result-in-hours. Label: conversion-result-in-hours-converted.
Set a data value. Selector: node:field-tijden:duration (I'm not sure where the :duration is coming from)
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.