Forums

Calculate nbr of days between two datepickers

SPABO 29 Oct, 2013
Trying to get a field in a form which shows the nbr of days between two datepickers, as soon as a valua in the 2nd has choosen

Field1: Arrivaldate
Field2: Departdate
Field3: Days>>To be showns as soon as Field2 has a value

Hope someone could be of assistance

Tx in advance

Rgds
S
GreyHead 29 Oct, 2013
Hi SPABO,

You can do it using the On Success events of the DatePickers to run a JavaScript function - or this FAQ might help.

Bob
SPABO 30 Oct, 2013
I only have the standard elements in.

Any place to find an example? The FAQ isn't helpfull
GreyHead 30 Oct, 2013
Hi SPABO,

I guessed you only had the standard elements in; but the RangePicker settings are designed to handle exactly what you are trying to do here.

Please see this FAQ which shows how to link two separate datepickers.

Bob
SPABO 30 Oct, 2013
Hi Bob,
Used this code snippet I found:
<?php
$date_from = $form->data['fromdate_id'];
$date_to   = $form->data['todate_id'];
$date_from = strtotime($date_from);
$date_to   = strtotime($date_to);
$days = abs($date_to - $date_from);
$days = $days/(60*60*24);
$form->data['totaldays_id'] = $days;
?>


To calculate the "nights", I changed
$days = $days/(60*60*24);
to
$days = ($days/(60*60*24))-1;


The value loaded in a "hiiden"field, the Customcode in the section OnSubmit

Tx again!
Rgds
K
comunicante 04 Feb, 2014
Hi!
I need exactly the same calculation in a form in Chronoform V 3.2 (in one old joomla 1.5 site)
I tried with Spabo code, but no luck.

Php code in On submit - before sending email
<?php
$date_from = $form->data['arrival_date'];
$date_to   = $form->data['departure_date'];
$date_from = strtotime($date_from);
$date_to   = strtotime($date_to);
$days = abs($date_to - $date_from);
$days = ($days/(60*60*24))-1;
$form->data['total_nights'] = $days;
?>


hidden field at the end of the form
<input id="total_nights" name="total_nights" type="hidden" value=""/>


html code in email template
notti:{total_nights}


But the email (and the field in the connected table) are empty
notti:


It's the php code ok for this version of Chronoform? Or where is my error?
Thank you for any suggestions!
comunicante 22 Mar, 2014
Nobody can give me a hint?
Tanks!
GreyHead 23 Mar, 2014
Hi comunicante,

You need to debug the PHP - the first step is to add a Debugger action to the form On Submit event and see what results you are getting for those values.

Bob
This topic is locked and no more replies can be posted.