1

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)

1 Answer 1

3

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:

  1. 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)

  2. Calculate the difference between the two integers and store in a variable

  3. Divide the result by x number of seconds, depending on what you want to calculate. To calculate days, divide by 86400 (60 * 60 * 24)

  4. 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.

12
  • Thank you for your answer! Can you please expand step 1 a little bit? When I go to Rules I have to choose a lot of settings and I'm not sure which to choose. Also what do you mean with "both data objects"? Do you mean the start and end time?
    – Kirsten
    Commented Feb 24, 2015 at 14:07
  • I was a little quick with asking for more explanation. I think I followed all the steps, but when I go to a node (and edit and save it) nothing happens. I changed my question with the steps I took in Rules.
    – Kirsten
    Commented Feb 24, 2015 at 14:43
  • Two things: to calculate results in minutes you need to DIVIDE (not multiple) seconds by 60. Same thing to calculate hours. Second thing: in the last step "Set data value" you need to store the result in a SEPARATE field, not in the date field. Your node needs to have an int field (called, for example "Difference in hours") to store the difference. The date field will not store the difference
    – argiepiano
    Commented Feb 24, 2015 at 16:19
  • Oeps, you're right, I changed it to divide. I also made an integer field and have te last action (Set data value) use that field. I then created a node, but still nothing happens. All I see is the date field I filled and the "Difference in hours" field is visible. I guess I'm still doing something wrong.. Any more ideas?
    – Kirsten
    Commented Feb 24, 2015 at 19:29
  • 1
    Kirsten, I put together a step-by-step video here: youtube.com/watch?v=s0muAp2rJbk
    – argiepiano
    Commented Feb 26, 2015 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.