• 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 Age between two dates

Calculate Age between two dates 8 years 10 months ago #35169

Hello.

I have this to calculate the age using the BirthDay field and the actual date ... and it works fine...
$myBdField = $_POST['form']['my-bday-field-name'];
 
if(date("md") < date("md", date("U", mktime(0, 0, 0, $myBdField['m'], $myBdField['d'], $myBdField['y'])))){
  $_POST['form']['my-hidden-field-name'] = (date("Y") - $myBdField['y']) - 1;
}else{
  $_POST['form']['my-hidden-field-name'] = date("Y") - $myBdField['y'];
}

But i want to calculate the age, using the BirthDay field and a future date.

Any ideas?

Thanks
The administrator has disabled public write access.

Calculate Age between two dates 5 years 7 months ago #39441

  • Gerard.Verheij
  • Gerard.Verheij's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 12
  • Thank you received: 4
I developed a small script to do this with JavaScript in RSForm:

I have a field "Birthdate" (of fieldtype birthday, so with the three picklists for day, month and year), and a hidden field "Age"

At the Submit-button, I call the function: Calculate_Age()
So, in the Additional Attributes field of your submit-button, put this:
OnClick="Calculate_BMI_color();Calculate_Age();"
(the first function Calculate_BMI_color() is not relevant in this topic.)

Then in the Form's CSS/Javascript field I have the following script with the function Calculate_Age():
<script type="text/javascript">
function Calculate_Age() {
  var today = new Date();
 
  Birth_year = Number(document.getElementById('Birthdatey').value);
  Birth_month = Number(document.getElementById('Birthdatem').value);
  Birth_day = Number(document.getElementById('Birthdated').value);
 
  var age = today.getFullYear() - Birth_year;
  var m = today.getMonth() - Birth_month;
 
  if (m < 0) {
     age = age - 1;
  }
 
  document.getElementById('Age').value = age.toString();
}
</script>

The age is stored in the hidden field "Age"
Last Edit: 5 years 7 months ago by Gerard.Verheij.
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!