Community Documentation

Calculating average speed

Last updated March 25, 2009. Created by jbrauer on June 4, 2007.
Edited by cbow, venkat-rk. Log in to edit this page.

D'Arcy Norman has a Computed Field example in which he calculates the average speed of a bike trip, rather than filling it in manually:

Computed Field settings (leaving other fields at their defaults):

  • Computed Code:$node_field[0]['value'] = ($node->field_distance[0]['value'] / $node->field_duration[0]['value']) * 60;
  • Check 'Store using the database settings below
  • Data Type: float
  • Data Length: 10,2
  • Default Value: 0
  • Check 'Sortable' to make the field sortable in views.

The end result can then be displayed in a sortable view.

Comments

What is the type of the duration field?

Hello! What is the type of the duration field? I would like to make a speed calculator, but i don't know the type of duration. I would like to use time format, like this: hh:mm:ss
Is it possible?

That is a REALLY good

That is a REALLY good question. I am curious what Darcy used as well... because the Computed Field Module does NOT work with the module, "Duration". I used the Module "HH:MM:SS" and that is working well now.

Unfortunately, it will calculate 6:30 as 6.5... which stinks...

About this page

Drupal version
Drupal 5.x, Drupal 6.x
Audience
Programmers, Site administrators

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.