• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Calculate a date field from 2 other form fields

Calculate a date field from 2 other form fields 10 years 3 months ago #30024

I'm looking for some help with setting a date field as the product of two other form fields, preferably in real time while the form is being filled.

Assuming fields

Date1
Date2
Period in months

I'm looking for something which sets Date2 = Date1 + Period in months." months"

Can anyone help please?
Last Edit: 10 years 3 months ago by michael.van-gent. Reason: Making the title clearer
The administrator has disabled public write access.

Calculate a date field from 2 other form fields 10 years 3 months ago #30045

  • cosmin.cristea
  • cosmin.cristea's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 756
  • Thank you received: 144
Please take a look at the following example, although it might not be exactly what you are looking for, it provides more than enough information to kickstart your project:

https://www.rsjoomla.com/support/documentation/view-article/918-how-to-calculate-the-difference-in-days-between-two-calendar-dates.html
My help is not official customer support. To receive your support, submit a ticket by clicking here
The administrator has disabled public write access.

Calculate a date field from 2 other form fields 10 years 2 months ago #30581

Thanks for the steer, but it seems overly complicated and is off topic - my PHP skills just don't extend that far.

The FAQ topic 'Date Operation'comes close, but I can't resolve the changes myself. Can some kind/skilled PHP programmer help with substituting 'Field 1' for the current date in the following code? I can change 'days' to 'months' myself. ;)
 
if(isset($_POST['form'])){
$_POST['form']['Field3'] = date('m/d/Y',strtotime(' +'.$_POST['form']['Field2'].' days'));
}
 

Many thanks
The administrator has disabled public write access.

Calculate a date field from 2 other form fields - 10 years 1 month ago #30617

Solved!

Got some help from someone infinitely more knowledgeable (Thanks Cosmin!) who came up with this solution:

Three fields : Start Date, Expiry Date, Duration.

Duration is a dropdown field with differing values, and the Expiry Date is calculated from Start Date plus Duration.

The Duration field is defined like this:

3 months | whatever label you want
6 months | whatever label you want
9 months | whatever label you want
etc

And in the scripts called on form process, insert this snippet:

$_POST['form']['expiry_date_field_name_goes_here'] = date('m/d/Y',strtotime($_POST['form']['start_date_field_goes_here'].' + '.$_POST['form']['duration_field_goes_here'][0]));

Notice the [0], do not delete it. It's mandatory when using dropdown fields, apparently, but I'm not sure why.

This worked (very elegantly) for me.
The administrator has disabled public write access.
The following user(s) said Thank You: cosmin.cristea

Calculate a date field from 2 other form fields - 10 years 1 month ago #30618

  • cosmin.cristea
  • cosmin.cristea's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 756
  • Thank you received: 144
I apologize for not being more explicit about the [0].

Due to the nature of the field (being a dropdown, it might have more than one value), the
$_POST['form']['dropdown']
is an array that can hold multiple elements.

However, for this case, using a single-selection dropdown, you need to access only the first one, which has the index [0].

I hope this helps.

PS: Thank you for taking the time to share it with fellow community members.
My help is not official customer support. To receive your support, submit a ticket by clicking here
Last Edit: 10 years 1 month ago by cosmin.cristea.
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!