Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Calculate age in months

More
18 hours 20 minutes ago #273169 by Myriam123
Please help us help you and fill where relevant:
Your LimeSurvey version: Version 6.15.23+251110 
Own server or LimeSurvey hosting:
Survey theme/template:
==================
Hi,
I have a question asking for the date of birth in dd/mm/yyyy format. I would like to calculate the age in months from that question to then create conditions to only show certain following questions depending on their age in months.
Thank you 

Please Log in to join the conversation.

More
2 hours 52 minutes ago #273171 by Joffm
Replied by Joffm on topic Calculate age in months
Hi,
what is your real concern?
a. How to calculate a difference generally or
b. how to calculate the difference in month?

a. This is rather easy.
The output of the date question is a string like 25/10/2023.
Now you use the function "strtotime(date)" to convert this string into a number, that outputs the number of seconds since 1/1/1970.

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)

Now you need the actual date.
You do the same. Convert the actual date into this timestamp.
You may use
  • either  {strtotime('now')} (These verbatims you find in the manual: Question type "date")
  • or {strtotime(date('Y-m-d'))} (here the seconds since midnight are not taken into account)
To get the difference you subtract the two values and get the difference in seconds.
To get the difference in days you only have to divide by 86400.
{(strtotime('now')-strtotime(Q1))/86400}
Fine.

And now the reason why I asked about the difference in month.
Unfortunately there are months with 28 days and months with 31 days. So you can divide the number of days by 30.42 (what is the average length of a month in a regular year)
But this is up to you.
 

That there are no whole numbers of days is due to the switches from daylight saving time to standard time and viceversa.

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose