[pmwiki-users] Using PTVs inside php on a page for calculating days difference between dates

kirpi at kirpi.it kirpi at kirpi.it
Thu Sep 25 10:59:36 CDT 2014


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="...":)

I could not find a "ready" way among the pmwiki.org pages (my fault?)
for calculating differences between dates, yet by browsing the web it
seems that php has a tool just for this[1]
Thus I tried and insert into the form page, as a test

  (:html:)<?php
  $date1 = new DateTime("20070324");
  $date2 = new DateTime("20070327");
  $interval = $date1->diff($date2);
  echo $interval->days;
  ?>(:htmlend:)

which seems to work quite fine :-)
Now the problem was to insert the PTVs inside php and by browsing
pmwiki.org I found that there could be a way[2][3]
So I tried this

  ||Check_in: 20070324  ||
  ||Check_out: 20070327 ||

  (:html:)<?php
  $pagename = ResolvePageName($pagename);
  $in=PageVar($pagename,'$:Check_in');
  $out=PageVar($pagename,'$:Check_out');
  $date1 = new DateTime($in);
  $date2 = new DateTime($out);
  $interval = $date1->diff($date2);
  echo $interval->days;
  ?>(:htmlend:)

but, despite several variations (sort of random, I admit), it seems
that it does not work.
My intention was, in case of success, to build something like

  (:input hidden name=$:Number_of_nights value="(:html:)<?php .....
?>(:htmlend:)":)

I understand that most of the people in this community would solve it
in seconds, yet to me it took days just to arrive at this point.
So, now being stucked, I just wanted to know if it is worth insisting
or it is just the wrong way.
Would it be too far from decency?
Would it work or mine are just days spent for nothing, please?

Thanks!

Luigi

----
[1] http://php.net/manual/en/datetime.diff.php
[2] http://www.pmwiki.org/wiki/PmWiki/PageTextVariables#withincode
[3] http://www.pmwiki.org/wiki/Cookbook/Functions#PageVar



More information about the pmwiki-users mailing list