Community Documentation

Calculating total time taken to finish a race

Last updated November 27, 2012. Created by a_thakur on September 3, 2012.
Edited by carnauro. Log in to edit this page.

The following method illustrates how to calculate, time taken by a rider to finish a race.
Terminolgies

  • Time taken(field_time_taken): Time taken by a rider to finish the race, which is an arithmetic operation of start time, stop time and penalities.
  • Start time(field_start_time): The start time of the race.
  • Stop time(field_stop_time): Time at which the rider finishes the race.
  • Penalties(field_penalties): Penalties(in seconds).

So Time taken = Stop time - Start time + Penalties

In Drupal 7, $node variable can no longer be used instead $entity has to be used.
The following code, would go into the "Computed Code(PHP)" section of the field settings.

<?php
  $node
= menu_get_object();
 
$start_time = field_get_items('node', $node, 'field_start_time');
 
$stop_time = field_get_items('node', $node, 'field_stop_time');
 
$start_time = $start_time[0]['value'];
 
$stop_time = $stop_time[0]['value'];
 
$penalties = field_get_items('node', $node, 'field_penalities');
 
$penalties = $penalties[0]['value'];
 
$entity_field[0]['value'] = $stop_time - $start_time + $penalties;
?>

The following code would go into "Display Code (PHP)" section of the field settings.
<?php
  $display_output
= $entity_field_item['value'];
?>

Page status

Needs technical review

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Audience
Programmers, Site builders
Level
Intermediate, Advanced
Keywords
Drupal 7

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.
nobody click here