Calculate Next Date

davies401

New Member
Hi,
I have two fields one of which holds a number of months, and the other is a date field.
I want to add another field that adds the number of months to the date field.
The number of months is 'period' and the date is 'action date'. The new field will be 'next action'.

Appreciate any advice with this,

Many thanks in anticipation,

Alan.
 
You could either do it as a calc element, if you just want to display the date (and not treat it as a date element):

Code:
$myDate = new DateTime('{yourtable___action_date_raw}');
return $myDate->add(new DateInterval('P{yourtable___period_raw}W'))->format('Y-m-d');

... you'd probably want to add some sanity checking to make sure you have a date string and a number, and change 'Y-m-d' to whatever format you want displayed.

Or you could do it in a form submission plugin, if you want 'next_action' to be an actual date element ... run this in a PHP form plugin, 'onBeforeProcess' ...

Code:
$myDate = new DateTime($formModel->formData['yourtable___action_date_raw']);
$formModel->updateFormData(
   'yourtable___action_date',
   $myDate->add(new DateInterval('P' . $formModel->formData['yourtable___period_raw] . 'W'))->format('Y-m-d H:i:s'),
   true
);

Again, this is kind of shorthand code, you would probably want to sanity check. The problem with using DateTime is, if you hand it anything but a properly formatted date string, it'll blow up.

Oh, and obviously change yourtable in the element names to ... well, your table ... but leave the _raw on the end.

But that should get you going.

-- hugh
 
Hi Hugh,

Many thanks, much appreciated.

I think that I need this as an element because I want to be able to filter the list by the 'next action' date.
I've not used the PHP form plugin before, so I've downloaded and installed plg_fabrik_form_php_3.7.
I now have a form holding the PHP code called 'Next Date Calc', set OnBeforeProcess.
I'm currently getting an error when I view the asset list, but that's due to my lack of knowledge and typing.

The calculation I want to set is
assets_register___last_inspection + assets_register__interval = assets_register__next_inspection
These elements are date + field = date (and I have set the format to d-m-Y for the date fields).

Would you mind showing me what code I need to enter using the above fields?

Sorry to be a pain,

regards
Alan
 
Code:
$myDate = new DateTime($formModel->formData['assets_register___last_inspection_raw']);
$formModel->updateFormData(
   'assets_register__next_inspection',
   $myDate->add(new DateInterval('P' . $formModel->formData['assets_register__interval_raw] . 'W'))->format('Y-m-d H:i:s'),
   true
);

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top