[pmwiki-users] Using PTVs inside php on a page for calculating days difference between dates
Hans Bracker
design at softflow.co.uk
Fri Sep 26 03:29:28 CDT 2014
Hello Luigi,
Thursday, September 25, 2014, 4:59:36 PM, you wrote:
> On a private, password locked form I have two input fields for a B&B
> check in and check out dates
> (:input text name=$:Check_in:)
> (:input text name=$:Check_out:)
> A third field/variable should be calculated, storing the number of
> days (nights, actually).
> Something like
> (:input hidden name=$:Number_of_nights value="...":)
>....
By using Fox form processing, I would create a Fox input filter to do
the php calculating part from your Check_in and Check_out values, and
setting Number_of_nights as preparation to add or update a ptv.
Fox input filter functions run before Fox main procesing of the input from the
form submission, so you can manipulate input in any way you like with
appropiate input filters. Documentation is here:
http://softflow.co.uk/design/FoxDocumentation/FilterFunctions
So here is what you could try:
in your Fox form:
(:input text name=$:Check_in:)
(:input text name=$:Check_out:)
(:input hidden foxfilter number_of_days:)
then add to your config.php somewhere before you include fox.php
something like this:
$FoxFilterFunctions['number_of_days'] = 'Fox_number_of_days';
function Fox_number_of_days($pagename, $fields) {
$date1 = new DateTime($fields['ptv_Check_in']);
$date2 = new DateTime($fields['ptv_Check_out']);
$interval = $date1->diff($date2);
$fields['ptv_number_of_days'] = $interval->days;
return $fields;
}
I used the php code you supplied and wrapped it into a function for
Fox.
Note: since this is php and the form submits field names starting with
$: as names with ptv_ as prefixw, we need to use the ptv_ prefixed
names. Fox will then process these as relating to PTVs, and add or
modify the corresponding PTVs. The array $fields corresponds to the
$fx array Fox uses internally, with the input from the form submitted
and some other values. For debug set in fox.php $FoxDebug = 5; for
instance, and you see $fx and how it gets changed in the process and
how the foxfilter function(s) get handled.
Hope this helps!
cheers,
Hans
mailto:design at softflow.co.uk
www.softflow.co.uk
More information about the pmwiki-users
mailing list