Forums

Convert minutes to hours

Echo 24 Jul, 2011
Hi,
I have a rather simple question: In my form the user inputs to a textbox the duration of an activity in minutes, and I would like to store it in the DB table as hours. So simply to divide the user's input by 60 and preferably round it to 2 digits. Is there a simple way to do that? I tried to play a bit with JS code but but I'm not sure how to refer to the textbox, or change it.

Thanks!
GreyHead 25 Jul, 2011
Hi Echo,

You can do this after the form is submitted with a little PHP (in the OnSubmit Before Email box in CFv3, or a Custom Code box in CFv4).
<?php
$minutes = JRequest::getInt('some_input_name', '', 'post');
$hours = '';
if ( $minutes ) {
  $hours = round($minutes/60, 2);
}
// Uncomment the next line in CFv3
//JRequest::setVar('hours', $hours);
// Uncomment the next line in CFv4
//$form->data['hours'] = $hours;
?>

Bob
Echo 25 Jul, 2011
It works wonderfully. At first nothing changed but I noticed there was a missing ')' after the isset statement and that in last line I should have the same input name, so the working code is as follows:

<?php
$minutes = JRequest::getInt('input_name', '', 'post');
$hours = '';
if ( $minutes ) {
  $hours = round($minutes/60, 2);
}
if ( isset( $form->data )) {
  $form->data['input_name'] = $hours;
}
?>


Thanks bob, really appreciate your support 🙂
GreyHead 26 Jul, 2011
Hi Echo,

Apologoes for my typos. You can omit the whole 'if' statement. I was trying to be clever and add some code that would detect which ChronoForms version was running then chenged my mind and left the line in by mistake.

Bob

PS I've updated my original post.
This topic is locked and no more replies can be posted.