tags:

views:

307

answers:

2

I am using a popular module called "Computed Field" in Drupal. I have used this field to make on the fly computations for several quote systems. I'm trying to use it to calculate the difference in time for a timesheet type of application. So a user enter in two times into CCK date fields (using PHP custom input g:i - ie 10:20 - year added automatically) but for some reason, I can't get the computed field to show any calculations.

Here is the most simple test example...still wont work

$start_value = $node->field_ts_train_start[0]['value'];
$end_value = $node->field_ts_train_end[0]['value'];


$node_field[0]['value'] = $end_value - $start_value;

From here, we pass the variable to $display. Can anyone see why this isn't working? I am open to any suggestions on how to work around the computed field, or to build custom functions using integers instead of UNIX time stamps..

Thanks.

Tim

A: 

Use the timestamp key, not value. Value is the string representation (January 1st, 2010), while timestamp is the UNIX time (1234567890)

$start_value = $node->field_ts_train_start[0]['timestamp'];
$end_value = $node->field_ts_train_end[0]['timestamp'];
$node_field[0]['timestamp'] = $end_value - $start_value;
dmitrig01
That is good information to have,a s this is my first time trying to do calculations with time. However, it's still malfunctioning. If I change "$node_field[0]['timestamp']" to ....['value'], I get a zero, otherwise I get NULL (no field). What about the type of date field? I am using "Date", should I try Datetime or Datestamp?
cinqoTimo
Your answer isn't exactly true, there are some other variables to consider, such as the date field type, and granularity. I have solved this - posting resolution as answer..
cinqoTimo
+1  A: 

The key to this was to use the date stamp CCK field with custom input field. The date stamp should include all levels of time, with the custom input only collecting HH:MM. This creates a full unix timestamp.

Then use a computed field to subtract start time from end time. This way the stamp is stored as an integer, and the time can be divided by 60 to get the amount of minutes.

$start_value = $node->field_ts_start_train[0]['value'];
$end_value = $node->field_ts_end_train[0]['value'];
$node_field[0]['value'] = $end_value - $start_value;
cinqoTimo