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:
<?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->diff($date_end);
$entity_field[0]['value'] = $interval->format('%h');
?>
<?php
$display_output = $entity_field_item['value'] . " hours";
?>
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)